How to popup Alarm Message without any activity in background?

痞子三分冷 提交于 2019-12-13 06:32:20

问题


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.



来源:https://stackoverflow.com/questions/10720181/how-to-popup-alarm-message-without-any-activity-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!