지금부터 3 일 후 꺼지기를 원하기 때문에 알람을 설정하려고합니다. 그 전에 안드로이드가 내 앱을 죽일 것입니다. 내 활동에영구 알람
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);
}
매니페스트 :
<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();
}
나는 오랜 시간 후에 알림을 수신하지 않습니다. 예를 들어 오전 3시에 11시에 알람을 설정 했으므로 알람이 울리면 알림이 표시됩니다. 그러나 알림이 표시되지 않습니다.
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);
}
}
}
참고 : 알람 네트워크 작업을 수행 할 경우, 다음 다운로드를 시작
'ContactAlarmService'가'Service' 인 것처럼 보이지만 브로드 캐스트를 보내는'PendingIntent'를 사용하고 있습니다.'PendingIntent.getBroadcast()'. –
알람이 울리면 내가 발사하길 원하는 보류 의도가 방송입니다. –
그러면 대상으로 'BroadcastReceiver' 클래스가 필요합니다. 아마도 구체적으로'WakefulBroadcastReceiver' 일 것입니다. 그런 다음 '서비스'를 시작하십시오. –