Вопрос

I am using the pending intent in activity.

Intent intent = new Intent(this, MyActivity.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 34433,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis() + (5000), pendingIntent);

here i am going from one activity(MainActivity) to another activity(MyActivity). but it is not working.

if i try to do it with simple Intent then it works fine.

Это было полезно?

Решение

You have requested a PendingIntent to start a BroadcastReceiver, but you are trying to start an Activity. This can't work.

If you want to start an Activity, instead of this:

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 34433,
        intent, PendingIntent.FLAG_UPDATE_CURRENT);

do this:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 34433,
        intent, PendingIntent.FLAG_UPDATE_CURRENT);

Другие советы

You can also use broadcast receiver with PendingIntent to start another activity.

create another class for example receiver as sub class of the BroadcastReceiver

public class receiver extends BroadcastReceiver {


     @Override
     public void onReceive(Context context, Intent intent) 
     {   
        // code to start another activity
         Intent intent = new Intent(context.getApplicationContext(), MyActivity.class);
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(intent);
     }
  }

and define MyActivity as receiver in android menifest file and in main activity change this code MyActivity.class to receiver.class

Intent intent = new Intent(this, receiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 34433,
        intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,
        System.currentTimeMillis() + (5000), pendingIntent);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top