如何在更新应用后在后台运行服务

3
我有一个闹钟应用程序,我注意到当我更新/重新安装应用程序时,待处理的闹钟意图会被删除。因此,我需要一种方法来检测应用程序是否已更新,而不需要用户启动应用程序。
我使用广播来检测手机重启以重新注册我的闹钟,但不确定如何检测应用程序何时更新。
例如,是否有任何方法可以在应用程序安装后立即运行服务,而无需用户打开应用程序?
我已经检查过这些内容,但似乎不可能。但想知道是否有更新的Android版本: 在设备上安装应用程序时自动启动服务 如何在第一次安装.apk时启动服务 谢谢。
2个回答

3

与其设置闹钟,不如注册一个特定于此目的的清单BroadcastReceiver。它可以监听android.intent.action.PACKAGE_REPLACED(注意:该常量中缺少“action”一词)或android.intent.action.MY_PACKAGE_REPLACED。

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

重新安装后,您的接收器将会收到这个消息,然后您就可以开始一个活动了。

谢谢Tariq。不知道这个意图。会试一下。 - madu

1

请尝试以下操作:

不过,首先让我们定义一个广播接收器,它将由闹钟执行,并启动我们的IntentService:

public class MyAlarmReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 12345;
public static final String ACTION = "com.codepath.example.servicesdemo.alarm";

 // Triggered by the Alarm periodically (starts the service to run task)
 @Override
 public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyTestService.class);
i.putExtra("foo", "bar");
context.startService(i);
 }
 }

接下来,让我们在AndroidManifest.xml中注册我们的IntentService和MyAlarmReceiver。
<receiver
android:name=".MyAlarmReceiver"
android:process=":remote" >
</receiver>

<service
android:name=".MyTestService"
android:exported="false" />

Java活动

public class MainActivity extends Activity {
@Override
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scheduleAlarm();
 }

// Setup a recurring alarm every half hour
 public void scheduleAlarm() {
 // Construct an intent that will execute the AlarmReceiver
 Intent intent = new Intent(getApplicationContext(), MyAlarmReceiver.class);
 // Create a PendingIntent to be triggered when the alarm goes off
 final PendingIntent pIntent = PendingIntent.getBroadcast(this, 
  MyAlarmReceiver.REQUEST_CODE,
    intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Setup periodic alarm every every half hour from this point onwards
long firstMillis = System.currentTimeMillis(); // alarm is set right away
AlarmManager alarm = (AlarmManager) 
 this.getSystemService(Context.ALARM_SERVICE);
// First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, 
 RTC_WAKEUP
// Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, 
 INTERVAL_HOUR, INTERVAL_DAY
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis,
    AlarmManager.INTERVAL_HALF_HOUR, pIntent);
   }
   }

设置完闹钟后,如果我们想要取消闹钟,可以这样做:

public void cancelAlarm() {
Intent intent = new Intent(getApplicationContext(), MyAlarmReceiver.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, MyAlarmReceiver.REQUEST_CODE,
   intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pIntent);
 }

并检查您的清单权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

我不确定,但请检查一下,也许可以帮助你


非常感谢A.Geek。我现在需要一种方法来查找应用程序何时更新,因为这似乎会使挂起的意图无效。 - madu

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