0

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..

vinothp
  • 9,939
  • 19
  • 61
  • 103

2 Answers2

3

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);
tckmn
  • 57,719
  • 27
  • 114
  • 156
Vipul Purohit
  • 9,807
  • 6
  • 53
  • 76
  • Actually i have done this.. thats what i am doing now but the problem is while Alarm Popup message i am seeing the last used screen on background which i don't need... – vinothp May 23 '12 at 13:29
  • Then why don't you finish your pin activity when alarm popup? – Vipul Purohit May 23 '12 at 13:43
  • thats what problem.. is it possible to close the activity from another activity.. – vinothp May 23 '12 at 14:01
  • Yes it's possible. Just use the context of the activity which you want to close and call finish() method wherever you want : `ACTIVITY_CONTEXT.finish()` – Vipul Purohit May 24 '12 at 04:58
  • Thanks for your comment.. Sorry i haven't got any idea about getting the context of Activity.. could you help me out please – vinothp May 29 '12 at 13:54
  • In an easy manner, do something like this : **Set a public static variable in your global class like :** `public static Context mPinContext;` **Now in your pin activity on OnCreate method set the mPinContext to pin activities context :** `Global.mPinContext = this`; **Now when you want to finish the pin activity when alarm popup write something like this :** `((Activity)Global.mPinContext).finish();` and this will finish you pin activity. **Insted of settign a global Context you can also pass Activity context by creating a method** – Vipul Purohit May 30 '12 at 04:54
  • thanks.i tried your approach but i am facing java.lang.ClassCastException.. **`public class TimeAndExpensesApplication extends Application{ public static Context context; public void onCreate(){ super.onCreate(); TimeAndExpensesApplication.context = getApplicationContext(); } public static Context getAppContext() { return TimeAndExpensesApplication.context; } }`** In PinActivity onCreate `TimeAndExpensesApplication.context = this;` **In AlarmPopupActivity onCreate**`((ReminderPopupMessage)TimeAndExpensesApplication.context).finish();`thanks – vinothp May 30 '12 at 08:38
  • **Do it like this** **// Create a global class** `public class Global { public static Context context; }` **// Set Context** `public class YourClass extends Activity { public void onCreate() { super.onCreate(); Global.context = TimeAndExpensesApplication.this; }` **// Get Context just direct use** `((Activity)Global.context).finish()` – Vipul Purohit May 30 '12 at 08:48
  • Thanks Vipul, What i done is in `((Activity)Global.context).finish` instead of **Activity** i used **CurrentActivity** name.. Now i changed to Activity..I solved the issue and got the expected answer..Thanks for your time.. – vinothp May 30 '12 at 09:19
1

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.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Ronak Gadhia
  • 524
  • 5
  • 12