3

我正在尝试设置一项服务,以检查何时在设备中安装了新的活动更新。我已经在应用程序活动中这样做了Broadcastreceiver,在清单中声明它并且它工作得很好。

但是,当我尝试在服务中运行该接收器并动态声明它时,我onReceive永远不会被调用。这是我的服务代码:

public class UpdateService extends Service {    
 private static String mPackage = "com.my.package";
 private static String mActivityName = "myActivity";
 private BroadcastReceiver mUpdateReceiver; 

 @Override
 public void onCreate() {
  super.onCreate();
  mUpdateReceiver = new BroadcastReceiver() {

   @Override
   public void onReceive(Context context, Intent intent) {
    Log.d("mTag","In the BroadcastReceiver onReceive()");
    if (intent.getAction().equalsIgnoreCase(Intent.ACTION_PACKAGE_REPLACED)) {
     // Log that a new update is has been found
     Log.d("mTag","New version of the app has been installed.");
     Log.d("mTag", "Intent data: " + intent.getDataString());
     Log.d("mTag","My package: " + mPackage);
    }
   }
  };

  Log.d("mTag","In the service onCreate() method.");
  IntentFilter filter = new IntentFilter();
  filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
  filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
  filter.addAction(Intent.ACTION_PACKAGE_ADDED);
  registerReceiver(mUpdateReceiver,filter);
 }

 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  Log.d("mTag","UpdateService started");            
  return Service.START_STICKY;
 }

 @Override
 public void onDestroy() {
  super.onDestroy();
  unregisterReceiver(mUpdateReceiver);
  Log.d("mTag","Service destroyed");
 }

 @Override
 public IBinder onBind(Intent arg0) {
  return null;
 }
}

服务中的日志 onCreate() 被调用,它告诉我该服务已启动并正在运行。但是,通过 adb 安装和替换某些应用程序后,BroadcastReceiver该方法中的任何日志都onReceive()不会被调用。

这是我的主要活动:

public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  startService(new Intent(this, UpdateService.class));
 }
}

你们有什么想法为什么onReceive()不被调用?

谢谢你。

我的代码基于这两个参考:

4

2 回答 2

5

您应该将数据模式添加到您的 IntentFilter。

 IntentFilter filter = new IntentFilter();
  filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
  filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
  filter.addAction(Intent.ACTION_PACKAGE_ADDED);

  filter.addDataScheme("package");

  registerReceiver(mUpdateReceiver,filter);
于 2013-06-08T01:37:19.817 回答
2

如果您尝试收听ACTION_PACKAGE_REPLACED广播,则无法通过服务完成。最有可能的是,当您的应用程序关闭时会发生替换。这就是为什么你不会听它的原因。

您应该从 注册,Manifest让您的操作系统知道您想收听这个Intent然后创建一个extentsBroadcastReceiver 的类,如下所示:

显现:

 <receiver android:name="PackageChangeReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <data android:scheme="package" />
        </intent-filter>
    </receiver>

接收者:

public class PackageChangeReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED) && intent.getDataString().contains(context.getPackageName())) {
        Log.d("Tag", "Package has been replaced");
        Log.d("Tag", "Intent data: " + intent.getDataString());
        Log.d("Tag", "Action: " + intent.getAction());
    }
}
}

我已经检查过是否intent.getDataString().contains(context.getPackageName())确保更换包是我的,而不是任何其他应用程序。

于 2014-02-09T13:23:19.473 回答