简体   繁体   中英

How to start a new activity from a broadcast receiver

I have a BroadcastReceiver like this

    public class OnAlarmReceiver extends BroadcastReceiver {

private static final String TAG = ComponentInfo.class.getCanonicalName(); 


@Override   
public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "Received wake up from alarm manager.");

    long rowid = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID);

    WakeReminderIntentService.acquireStaticLock(context);

I wanted to start a new activity from this. How can it be done? I tried using startActivty(). But that didn't worked. Please help. Thanks in advance

Use this code in your onRecieve method:

Intent activityToLaunch = new Intent(context, YourActivity.class); 
Context.startActivity(intent);

Try something like this:

public class OnAlarmReceiver extends BroadcastReceiver {

  private static final String TAG = ComponentInfo.class.getCanonicalName(); 

 @Override   
 public void onReceive(Context context, Intent intent) {
 Log.d(TAG, "Received wake up from alarm manager.");r

 long rowid = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID);

 WakeReminderIntentService.acquireStaticLock(context);

 Intent yourActivity = new Intent(getBaseContext(),your.class);
 startActivity(youActivity);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM