Android安装与卸载应用

原创 2012年03月30日 00:13:51

 


/**

 * Demonstration of styled text resources.

 */

public class InstallApk extends Activity{

    static final int REQUEST_INSTALL = 1;

    static final int REQUEST_UNINSTALL = 2;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

 

        setContentView(R.layout.install_apk);

 

        // Watch for button clicks.

        Button button =(Button)findViewById(R.id.unknown_source);

        button.setOnClickListener(mUnknownSourceListener);

        button = (Button)findViewById(R.id.my_source);

        button.setOnClickListener(mMySourceListener);

        button = (Button)findViewById(R.id.replace);

        button.setOnClickListener(mReplaceListener);

        button = (Button)findViewById(R.id.uninstall);

        button.setOnClickListener(mUninstallListener);

        button = (Button)findViewById(R.id.uninstall_result);

        button.setOnClickListener(mUninstallResultListener);

    }

 

    @Override

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {

        if (requestCode == REQUEST_INSTALL) {

            if (resultCode == Activity.RESULT_OK) {

                Toast.makeText(this, "Install succeeded!", Toast.LENGTH_SHORT).show();

            } else if (resultCode == Activity.RESULT_CANCELED) {

                Toast.makeText(this, "Install canceled!", Toast.LENGTH_SHORT).show();

            } else {

                Toast.makeText(this, "Install Failed!", Toast.LENGTH_SHORT).show();

            }

        } else if (requestCode == REQUEST_UNINSTALL) {

            if (resultCode == Activity.RESULT_OK) {

                Toast.makeText(this, "Uninstall succeeded!", Toast.LENGTH_SHORT).show();

            } else if (resultCode == Activity.RESULT_CANCELED) {

                Toast.makeText(this, "Uninstall canceled!", Toast.LENGTH_SHORT).show();

            } else {

                Toast.makeText(this, "Uninstall Failed!", Toast.LENGTH_SHORT).show();

            }

        }

    }

 

    private OnClickListener mUnknownSourceListener = new OnClickListener() {

        public void onClick(View v) {

            Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);

            intent.setData(Uri.fromFile(prepareApk("HelloActivity.apk")));

            startActivity(intent);

        }

    };

 

    private OnClickListener mMySourceListener = new OnClickListener() {

        public void onClick(View v) {

            Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);

            intent.setData(Uri.fromFile(prepareApk("HelloActivity.apk")));

            intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);

            intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);

            intent.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME,

                    getApplicationInfo().packageName);

            startActivityForResult(intent, REQUEST_INSTALL);

        }

    };

 

    private OnClickListener mReplaceListener = new OnClickListener(){

        public void onClick(View v) {

            Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);

            intent.setData(Uri.fromFile(prepareApk("HelloActivity.apk")));

            intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);

            intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);

            intent.putExtra(Intent.EXTRA_ALLOW_REPLACE, true);

            intent.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME,

                    getApplicationInfo().packageName);

            startActivityForResult(intent, REQUEST_INSTALL);

        }

    };

 

    private OnClickListener mUninstallListener = new OnClickListener() {

        public void onClick(View v) {

            Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);

            intent.setData(Uri.parse(

                    "package:com.example.android.helloactivity"));

            startActivity(intent);

        }

    };

 

    private OnClickListener mUninstallResultListener = new OnClickListener() {

        public void onClick(View v) {

            Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);

            intent.setData(Uri.parse(

                    "package:com.example.android.helloactivity"));

            intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);

            startActivityForResult(intent, REQUEST_UNINSTALL);

        }

    };

 

    private File prepareApk(StringassetName) {

        // Copy the given asset out into a file so that it can be installed.

        // Returns the path to the file.

        byte[] buffer = new byte[8192];

        InputStream is = null;

        FileOutputStream fout = null;

        try {

            is = getAssets().open(assetName);

            fout = openFileOutput("tmp.apk", Context.MODE_WORLD_READABLE);

            int n;

            while ((n=is.read(buffer)) >= 0) {

                fout.write(buffer, 0, n);

            }

        } catch (IOException e) {

            Log.i("InstallApk", "Failedtransferring", e);

        } finally {

            try {

                if (is != null) {

                    is.close();

                }

            } catch (IOException e) {

            }

            try {

                if (fout != null) {

                    fout.close();

                }

            } catch (IOException e) {

            }

        }

 

        return getFileStreamPath("tmp.apk");

    }

}

 

相关文章推荐

Android中实现静态的默认安装和卸载应用

这篇博文转自这里:原博链接,由于原博实在是太给力了,故而转载到自己博客来,意在和大家分享~ 最近好长时间都没有写blog了,主要是因为最近工作上的事以及下载Android源码的事耽误的(下载源码这...

Android 监听应用的安装和卸载

  • 2012年11月29日 18:11
  • 136KB
  • 下载

Android利用系统广播---监听应用程序安装和卸载

在android系统中,安装和卸载都会发送广播,当应用安装完成后系统会发android.intent.action.PACKAGE_ADDED广播。可以通过intent.getDataString()...

Android监听应用程序安装和卸载

第一、 新建监听类:BootReceiver继承BroadcastReceiver package com.rongfzh.yc; import android.content.Broa...

Android NDK开发(九)——应用监听自身卸载升级版,使用Inotify监听安装目录

在上一篇博客中,我们讲了一个小小的案例,用NDK监听应用程序自身卸载,并且打开内置浏览器加载用户调用页面。关于监听应用程序自身卸载的原理和实现方案可以在上篇博客中找到,地址是:http://blog....

Android静默方式实现批量安装卸载应用程序

前段时间做了一个批量安装卸载应用程序的小应用,由于安装卸载应用程序的部分API是隐藏的,所以必须在ubuntu下下载Android系统源码,并编译之后使用MM命令编译生成APK文件,其实也难。思路是这...

android应用程序的安装与卸载

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

Android-通过意图安装、卸载、打开指定的apk和进入指定程序的应用信息

重要代码如下: protected void installApk() { // 安装的意图在Android系统上层所有应用的源代码\packages\apps\PackageInstaller\...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Android安装与卸载应用
举报原因:
原因补充:

(最多只允许输入30个字)