安卓:BroadcastReceiver的应用程序安装/卸载 [英] Android: BroadcastReceiver on application install / uninstall

查看:212
本文介绍了安卓:BroadcastReceiver的应用程序安装/卸载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要安装apk文件和设置的广播接收器,为了赶有关安装的状态信息。

I want to install an apk file and set a broadcast-receiver in order to catch information concerning install status.

我有prepared一个BroadcastReceiver类:

I have prepared a BroadcastReceiver class :

public class newPackageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("DEBUG"," test for application install/uninstall");
    }

}

在主要活动,我先注册一个新的接收对象,那么应用程序实例化按钮安装。

In the main activity, I first register a new receiver object, then instanciate button for application install.

public void onCreate(Bundle savedInstanceState) {
...
IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_PACKAGE_ADDED);
        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
        filter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
        filter.addAction(Intent.ACTION_PACKAGE_INSTALL);
        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
        filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);

        receiver = new newPackageReceiver();
        registerReceiver(receiver, filter);
        ...

dlButton.setText(R.string.dl_button);
dlButton.setOnClickListener(new AppliDownloadOnClickListener(this ));   


@Override
public void onDestroy(){
     unregisterReceiver(receiver);
     super.onDestroy();
}

在我OnclickListener类,我把:

In my OnclickListener class, i put :

@Override
    public void onClick(View v) {

    // actually, the below process is in an asyncTask
    URL url;
    Intent promptInstall;

    try {
        url = new URL(apkurl);

        HttpURLConnection c = (HttpURLConnection) url.openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        c.connect();

        String PATH = Environment.getExternalStorageDirectory()+ "/download/";
        File file = new File(PATH);
        file.mkdirs();
        File outputFile = new File(file, "app.apk");
        FileOutputStream fos = new FileOutputStream(outputFile);

        InputStream is = c.getInputStream();

        byte[] buffer = new byte[1024];
        int len1 = 0;
        while ((len1 = is.read(buffer)) != -1) {
            fos.write(buffer, 0, len1);
        }

        fos.close();
        is.close();

        promptInstall = new Intent(Intent.ACTION_VIEW);
        promptInstall.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), "application/vnd.android.package-archive");

        if (promptInstall != null) {
            activity.startActivity(promptInstall);
        } else {
            ErrorDetails.displayToastMessage(activity,R.string.connection_error);
        }


    } catch (...) {
        ...
    }

}

通过上面的code(我已经缩小它),点击按钮时,会显示安装程序和应用程序完全安装的,但接收器类(newPackageReceiver)不会被调用。注册(registerReceiver)完成在onCreate方法和unregisterReceiver是所谓的的onDestroy方法,所以它768,16有效。你知道为什么吗?

With the above code (I have shrunk it), when button is clicked, installer is displayed and application is perfectly installed, but receiver class(newPackageReceiver) is never called. Registering (registerReceiver) is done in the onCreate method and unregisterReceiver is called in the onDestroy method, so it shoud be valid. Do you know why ?

感谢您的阅读!

推荐答案

您需要添加<一href="http://developer.android.com/reference/android/content/IntentFilter.html#addDataScheme%28java.lang.String%29"相对=nofollow>数据您的意图过滤方案。

You need to add the data scheme to your intent filter.

filter.addDataScheme("package");

此外, ACTION_PACKAGE_INSTALL 从来没有在使用。

这篇关于安卓:BroadcastReceiver的应用程序安装/卸载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆