How to popup Alarm Message without any activity in

2019-08-16 14:41发布

问题:

In my android i app i can alarm functionality and as well logout functionality. After setting my alarm time i am exiting the app by clicking the logout button.

I am using

         ExitActivity.this.finish();  
         Intent intent1 = new Intent(ExitActivity.this,PinActivity.class);  
         intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);   
         startActivity(intent1);  

         Intent intent = new Intent(Intent.ACTION_MAIN); 
         intent.addCategory(Intent.CATEGORY_HOME);  
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
         startActivity(intent); 

this code to exit the app,which goes to home pin screen and after that it launches the home screen. This is because when i am coming back to my app it launches the pinscreen. Alarm working exactly what i want but while alarm popup message it has the pinactivity in the background(which i don't want). I wan't to get rid out of the pin activity in the background.

This is my receiver class?

     public class ShortTimeEntryReceiver extends BroadcastReceiver{

     @Override
     public void onReceive(Context context, Intent intent) {

    Toast.makeText(context,"Alarm Working", Toast.LENGTH_SHORT).show();

     try {
         Bundle bundle = intent.getExtras();
         String message = bundle.getString("alarm_message");

         Intent newIntent = new Intent(context, ReminderPopupMessage.class);
         newIntent.putExtra("alarm_message", message);
         newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(newIntent);
        } catch (Exception e) {
         Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
         e.printStackTrace();

        }
}

How do i do that? Thanks for your help guys..

回答1:

You should use Alarm Manager to set alarms in Android. The alarm manager holds your alarm and fire an pending intent on alarm time.

First create a pending intent like this :

 pendingIntent = PendingIntent.getService(CONTEXT, ALARM_ID,  INTENT_TO_LAUNCH, 0);

Then use this pending intent to set an Alarm like this :

 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
 alarmManager.set(AlarmManager.RTC_WAKEUP, ALARM_TIME, pendingIntent);

This will start the pending intent at given time.

To remove an alarm you have to recreate the same Pending Intent with same ALARM_ID :

 alarmManager.cancel(pendingIntent);


回答2:

First create a pending intent like this :

 pendingIntent = PendingIntent.getService(context, alarm_id, Pass your data with intent,  PendingIntent.FLAG_UPDATE_CURRENT);

Set an Alarm like this :

 AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        //19 4.4and above api level
        am.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis() + AlarmManager.INTERVAL_DAY, sender);
 } else {
        //below 19 4.4
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis() + AlarmManager.INTERVAL_DAY, sender);
 }

This will start the pending intent at given time.

To remove an alarm you have to Use the same Pending Intent with same ALARM_ID:

am.cancel(pendingIntent);

Now You need to create One Service to catch your Alarm.