android应用程序的安装与卸载

jopen 10年前

程序的安装与卸载在实际开发中也很常见,尤其是安装,在版本更新中必然会有。直接上示例:

MainActivity:

               import java.io.File;                import android.app.Activity;        import android.content.Intent;        import android.net.Uri;        import android.os.Bundle;        import android.os.Environment;        import android.view.View;        import android.view.View.OnClickListener;        import android.widget.Button;        import android.widget.Toast;                public class MainActivity extends Activity implements OnClickListener {            private Button installBtn;            private Button uninstallBtn;                    @Override            protected void onCreate(Bundle savedInstanceState) {                super.onCreate(savedInstanceState);                setContentView(R.layout.main);                installBtn = (Button) findViewById(R.id.main_btn_install);                uninstallBtn = (Button) findViewById(R.id.main_btn_uninstall);                installBtn.setOnClickListener(this);                uninstallBtn.setOnClickListener(this);            }                    @Override            public void onClick(View v) {                if (v == installBtn) {                    install();                }                if (v == uninstallBtn) {                    uninstall();                }            }                    /**            * 安装应用程序            */            private void install() {                if (!Environment.getExternalStorageState().equals(                        Environment.MEDIA_MOUNTED)) {                    Toast.makeText(this, "请插入SD卡", Toast.LENGTH_SHORT).show();                    return;                }                File sdCardDir = Environment.getExternalStorageDirectory();                File file = new File(sdCardDir, "Sport.apk");                if (!file.exists()) {                    Toast.makeText(this, "文件未找到", Toast.LENGTH_SHORT).show();                    return;                }                Uri uri = Uri.fromFile(file);                Intent intent = new Intent(Intent.ACTION_VIEW);                intent.setDataAndType(uri, "application/vnd.android.package-archive");                startActivity(intent);            }                    /**            * 卸载应用程序            */            private void uninstall() {                Uri uri = Uri.parse("package:com.lovo.sport");                Intent intent = new Intent(Intent.ACTION_DELETE, uri);                startActivity(intent);            }                }  
</div> </div>

布局xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >            <Button            android:id="@+id/main_btn_install"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="安装" />            <Button            android:id="@+id/main_btn_uninstall"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="卸载" />        </LinearLayout>  
</div> </div>