Reputation: 731
I'm having a requirement where i need to open an activity at specific time and perform specific task even when the app is killed. That is even when the app is removed from multi-task window pane.
As of now i'm using the alarm manager to achieve this task as shown below.
Intent myIntent = new Intent(getBaseContext(),MyScheduledReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(),
0, myIntent, 0);
AlarmManager alarmManager
= (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
long interval = 60 * 1000; //
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), interval, pendingIntent);
finish();
The onReceive method is as below:
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent scheduledIntent = new Intent(context, MyScheduledActivity.class);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(scheduledIntent);
}
The problem is, it opens up the activity when the app is in background. Not when in closed or not in the back stack(mutli-task pane).
Please lighten me up. I'm struggling.
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidScheduledActivity"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidScheduledActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MyScheduledActivity" />
<receiver android:process=":remote"
android:name="MyScheduledReceiver" />
</application>
</manifest>
Upvotes: 1
Views: 1712
Reputation: 1985
start a service and your alarm code in that.Once the alarm is done start your application through notification or as per your logic.
Upvotes: 1
Reputation: 593
You should use Service, Open your activity through service. Service
Upvotes: 0