Reputation: 137
I am trying to setup an alarm that will remain on because I want it to go off 3 days from now and I know Android will kill my app before that.
In my Activity
private void setAlarm() {
//ContactAlarmReceiver contactAlarmReceiver = new ContactAlarmReceiver();
//contactAlarmReceiver.setAlarm(getApplicationContext(), dateInMillis, phonenumberET.getText().toString(), firstnameET.getText().toString());
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(MyConstants.BROADCAST_ACTION_CONTACT_ALARM);
alarmIntent.putExtra("phone", phonenumberET.getText().toString());
alarmIntent.putExtra("name", firstnameET.getText().toString());
PendingIntent pi = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
//am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMills, pi);
if (Build.VERSION.SDK_INT >= 23) {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
} else if (Build.VERSION.SDK_INT >= 19) {
am.setExact(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
} else {
am.set(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
}
ComponentName component = new ComponentName(this, ContactAlarmReceiver.class);
getPackageManager()
.setComponentEnabledSetting(component,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
Manifest:
<receiver
android:name=".ContactAlarmReceiver"
android:enabled="false">
<intent-filter>
<action android:name="com.example.johnbravado.CONTACTALARM" />
<action android:name="com.example.johnbravado.NOTIFLTBTN" />
<action android:name="com.example.johnbravado.NOTIFRTBTN" />
</intent-filter>
</receiver>
ContactAlarmReceiver:
@Override
@Override
public void onReceive(Context context, Intent intent) {
PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"com.example.johnbravado.zionwork");
wakeLock.acquire();
final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Bundle extras = intent.getExtras();
ComponentName receiver = new ComponentName(context, ContactAlarmReceiver.class);
PackageManager pm = context.getPackageManager();
if (intent.getAction().equals(MyConstants.BROADCAST_ACTION_CONTACT_ALARM)) {
int count = extras.size();
String num = extras.getString("phone");
String name = extras.getString("name");
sendContactNotification(context, MyConstants.BIG_NOTIFICATION_ID, num, name);
}
if (intent.getAction().equals(MyConstants.BROADCAST_ACTION_NOTIF_LTBTN)) {
sendSMS(context, extras.getString("phone"), extras.getString("msg"));
mNotificationManager.cancel(MyConstants.BIG_NOTIFICATION_ID);
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
if (intent.getAction().equals(MyConstants.BROADCAST_ACTION_NOTIF_RTBTN)) {
initiatePhone(context, intent.getExtras().getString("phone"));
mNotificationManager.cancel(MyConstants.BIG_NOTIFICATION_ID);
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
wakeLock.release();
}
I never receive the notification after long periods of time. For example, I set the alarm at 11pm for 3am, so when i wake up i should see the notification. But there is no notification displayed.
Upvotes: 1
Views: 661
Reputation: 1291
I would like to suggest You that, first of all create a class like:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("startalarm")){
intent = new Intent(context, Your_service.class);
context.startService(intent);
}
}
}
Note: If your alarm has to perform network task, then start a download service inside onRecieve()
method of your alarm broadcast.
Then,set AlarmReceiver
class in Intent.
Intent alarmIntent = new Intent(MyActivity.this, AlarmReceiver.class);
perhaps you will get out from your problem.
Upvotes: 2