3

I have an alarm app and I have noticed that when I update/reinstall the app, the the pending alarm intents get removed. So I need a way to detect the application is updated, without the user starting the app.

I use broadcasts to detect phone reboots to re-register my alarms, but not sure how to detect when the app is updated.

For example, is there any way to run a service soon as the application is installed, without the user opening the app?

I have checked these but seems it is not possible. But wondering if there is any update for newer Android versions:

Start a service automatically when app installed to device

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

Thank you.

madu
  • 5,232
  • 14
  • 56
  • 96

2 Answers2

3

Rather than setting an alarm, have a Manifest registered BroadcastReceiver specifically for this purpose. It can either listen to android.intent.action.PACKAGE_REPLACED (note: the word action is missing from this constant) or to 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>

After reinstall your receiver will get this message and you'll be able to start an Activity

Qandil Tariq
  • 539
  • 1
  • 3
  • 15
1

try this:

First though, let's define the BroadcastReceiver that will be executed by the alarm and will launch our 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);
 }
 }

Next, let's register both our IntentService and MyAlarmReceiver in the AndroidManifest.xml.

<receiver
android:name=".MyAlarmReceiver"
android:process=":remote" >
</receiver>

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

java activity

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

After setting an alarm, if we ever want to cancel the alarm like this:

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

And check your manifest permissions:

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

I am not sure but please check my be it helps you

Android Geek
  • 8,956
  • 2
  • 21
  • 35
  • Thanks a lot A.Geek. I now need a way to find out when the application is updated, because that seems to void the pending intents. – madu Apr 11 '18 at 08:35