广播接收器MY_PACKAGE_REPLACED从未被调用

5
我有以下的应用场景: 1)一个可以自动更新的应用程序 2)设备已经root 3)检查在线版本,如果有新版本,则下载“apk”文件并安装
一切都很好,但是在新版本安装后,应用程序没有重新启动。我尝试设置MY_PACKAGE_REPLACED广播接收器,但它从未被调用。应用程序安装新版本后停止运行,但应用程序中的接收器从未被触发。
我做错了什么?
代码如下: MANIFEST
<receiver android:name=".receivers.OnUpgradeReceiver">
            <intent-filter>
              <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
              <data android:scheme="package"/>
          </intent-filter>
        </receiver>

我尝试了带有DATA部分和不带DATA部分的接收器清单代码,但仍然无法正常工作!

广播接收器类

public class OnUpgradeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        final String msg="intent:"+intent+" action:"+intent.getAction();
        Log.e("OLE","RECEIVEEEEEEEEEEEEEEEEEEEEEED: "+msg);
    }}

应用程序更新部分

Process p;  
                    try {  

                        //Runtime.getRuntime().exec (new String[]{"su", "-c", "pm install -r " + apkLocation +  "party.net"});

                            // Preform su to get root privledges  
                            p = Runtime.getRuntime().exec("su");   

                            // Attempt to write a file to a root-only  
                            DataOutputStream os = new DataOutputStream(p.getOutputStream());  
                            os.writeBytes("/system/bin/pm install -r"+apkLocation+"\n");  

                            // Close the terminal  
                            os.writeBytes("exit\n");  
                            os.flush();  
                            try {  
                                    p.waitFor();
                                    if (p.exitValue() != 255) {  
                                            Log.e("OLE","Sucess :-)");
                                    }  
                                    else {  
                                        Log.e("OLE","Fail 1");
                                    }  

                            } catch (InterruptedException e) {  
                                Log.e("OLE","Fail 2");
                            }  
                    } catch (IOException e) {  
                        Log.e("OLE","Fail 3 "+e.getMessage());
                    }

问题已解决! 问题在于新安装的版本没有设置广播接收器,这一点超过了先前版本。


2
已解决!问题在于新安装的版本没有设置广播接收器,而该版本是在之前的版本之上安装的!!! - MTurPash
我无法让它工作 - 你在使用哪个安卓版本? - Marcin
我的也不工作,而且从Android O开始,我们不能使用PACKAGE_REPLACED INTENT。 - Kushan
好的,同样的问题可能会在一些华硕设备上出现,请参见这里 - Jackie Degl'Innocenti
Android.intent.action.PACKAGE_REPLACED已经被弃用,替换成了android.intent.action.MY_PACKAGE_REPLACED。(https://developer.android.com/reference/android/content/Intent#ACTION_MY_PACKAGE_REPLACED)此外,在清单接收器声明中不再需要<data android:scheme="package"/>。(https://dev59.com/JGcs5IYBdhLWcg3wp133#27756663) - ClayHerendeen
1个回答

0

如果新的应用程序没有运行,就没有办法接收到意图,最好的解决方案是使用另一个应用程序B来接收意图并运行新的应用程序A。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接