【问题标题】:How to run a service in background after updating the app更新应用程序后如何在后台运行服务
【发布时间】:2018-04-11 04:57:44
【问题描述】:

我有一个警报应用程序,我注意到当我更新/重新安装该应用程序时,未决的警报意图被删除。 所以我需要一种方法来检测应用程序是否已更新,而无需用户启动应用程序。

我使用广播来检测手机重启以重新注册我的闹钟,但不确定如何检测应用何时更新。

例如,有没有什么方法可以在安装应用程序后立即运行服务,而无需用户打开应用程序?

我已经检查了这些,但似乎不可能。但想知道是否有针对较新 Android 版本的更新:

Start a service automatically when app installed to device

How to start a Service when .apk is Installed for the first time

谢谢。

【问题讨论】:

    标签: android background-process


    【解决方案1】:

    与其设置警报,不如让 Manifest 专门为此目的注册 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>
    

    重新安装后,您的接收器将收到此消息,您将能够启动 Activity

    【讨论】:

    • 谢谢塔里克。不知道这个意图。会试试这个。
    【解决方案2】:

    试试这个:

    首先,让我们定义将由警报执行并启动我们的 IntentService 的 BroadcastReceiver:

    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。我现在需要一种方法来确定应用程序何时更新,因为这似乎会使待处理的意图无效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多