Reputation: 1954
I'm trying to launch an activity from a closed app, using the AlarmManager and a BroadcastReceiver. The "received" debug message is shown even when the app is closed or in background, which means that the AlarmManager is working like I want it to. But the CameraActivity is only launched when the app is active. Anyone got an idea why it's not working? Here's (some of) my code:
MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
startTimeStamp,
intervalTime,
pendingIntent);
Log.i("########################DEBUG", "started");
}
}
AlarmReceiver
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("########################DEBUG", "received");
Intent launchIntent = new Intent(context, CameraActivity.class);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
context.startActivity(launchIntent);
/* Tried this too, didn't work
PackageManager packageManager = context.getPackageManager();
Intent launchIntent = packageManager.getLaunchIntentForPackage(CameraActivity.class.getName());
context.startActivity(launchIntent);*/
}
}
CameraActivity
public class CameraActivity extends AppCompatActivity {
private final int CAMERA_REQUEST = 1;
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
image = findViewById(R.id.imageview);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (resultCode == RESULT_OK && requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
image.setImageBitmap(photo);
}
}
}
manifest.xml
<application
...
<activity android:name=".CameraActivity">
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver" />
</application>
Thanks in advance!
Upvotes: 1
Views: 3404
Reputation: 996
As per the documentation from Android 10, you cannot start an activity when your app is running in the background, except in very limited cases involving the user interaction.
The documentation guide presents notifications as an alternative for starting activities from the background. Then, this guide lists the specific cases where the restriction doesn't apply. It suggests displaying Time-sensitive notification to provide urgent information to the user instead of directly starting an activity or full-screen intent.
Upvotes: 3