I'm trying simply to start an activity from a service for days. This can't be that difficult! All I want is this:
1) Start an Activity from a background service (scheduled by AlarmManager). Currently I'm doing this with this code
Intent i = new Intent(this, MyDialogActivity.class);
i.putExtra(MyDialogActivity.TEXT, myObject.getText());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
2) Show this Activity wheter the root activity is on top, wheter the root activity was destroyed via the back button, wheter the root activity was paused via the home button
3) Show this Activity wheter the device is on standby or not (Activity should be shown after the user wakes up the device from standby)
4) Destroy the activity for sure and send a broadcast after the user saw the activity and pressed a button (including home- and back-button). Currently I'm doing this with this code (pm is PowerManager)
protected void onPause() {
if (pm.isScreenOn()) {
sendBroadcast(retValue);
if (!isFinishing()) {
finish();
} else {
moveTaskToBack(true);
}
}
super.onPause();
}
5) Prevent that this activity could be started from another point than the calling service. Currently I'm doing this with setting this attributes in the AndroidManifest:
android:name=".activities.MyDialogActivity" android:noHistory="true" android:excludeFromRecents="true"
But whatever I'm doing, I can't realize 2) and 3). Could anybody help me?
Thank you!
I had same issues with starting an activity from a service. I was trying to start the system voice dialer.
My issues were caused because I was using the Application Context (this.getApplicationContext()). Once I've changed to "this" it worked.
This code works for me :
Where I get the context using :