【问题标题】:Broadcast Receiver MY_PACKAGE_REPLACED never called广播接收器 MY_PACKAGE_REPLACED 从未调用
【发布时间】:2016-08-18 17:57:11
【问题描述】:

我有以下应用场景: 1) 自行更新的应用程序 2)设备已root 3)在线检查版本,如果新版本在线,它会下载“apk”文件并安装它

一切正常,但新版本安装后APP没有重启。我试图设置 MY_PACKAGE_REPLACED 广播接收器,但它从未被调用。应用程序安装新并停止,但应用程序中的接收器永远不会被触发。

我做错了什么?

代码: 清单

<receiver android:name=".receivers.OnUpgradeReceiver">
            <intent-filter>
              <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
              <data android:scheme="package"/>
          </intent-filter>
        </receiver>

我尝试了带有 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);
    }}

APP更新部分

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());
                    }

已解决! 问题是安装在上一个版本之上的新版本没有设置广播接收器!!!

【问题讨论】:

  • 已解决!问题是安装在前一个版本之上的新版本没有设置广播接收器!!!
  • 我无法让它工作 - 你在哪个 android 版本上使用它?
  • 我的也不行,现在从 android O 开始我们不能使用 PACKAGE_REPLACED INTENT
  • 好的,在某些华硕设备上可能会发现同样的问题,请参阅here
  • 仅供参考,android.intent.action.PACKAGE_REPLACED 已被弃用并被 android.intent.action.MY_PACKAGE_REPLACED (developer.android.com/reference/android/content/…) 取代,而且您似乎不需要 不再在清单接收者声明中。 (stackoverflow.com/a/27756663/3314615)

标签: android broadcastreceiver auto-update


【解决方案1】:

如果新应用没有运行,则无法接收新应用的意图,最好的解决方案是使用另一个应用-B 接收意图并运行新应用-A。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多