2017-01-14 2 views
0

지금부터 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); 

}

} 
} 

참고 : 알람 네트워크 작업을 수행 할 경우, 다음 다운로드를 시작

+0

'ContactAlarmService'가'Service' 인 것처럼 보이지만 브로드 캐스트를 보내는'PendingIntent'를 사용하고 있습니다.'PendingIntent.getBroadcast()'. –

+0

알람이 울리면 내가 발사하길 원하는 보류 의도가 방송입니다. –

+0

그러면 대상으로 'BroadcastReceiver' 클래스가 필요합니다. 아마도 구체적으로'WakefulBroadcastReceiver' 일 것입니다. 그런 다음 '서비스'를 시작하십시오. –

답변

1

나는 우선이 같은 클래스를 생성, 당신을 제안하고 싶습니다 내부의 서비스는 알람 방송의 방법 onRecieve()입니다. 그런 다음 인 텐트에 AlarmReceiver 클래스를 설정하십시오.

Intent alarmIntent = new Intent(MyActivity.this, AlarmReceiver.class); 

아마도 문제가 발생합니다.

+0

앱을 죽인 후에도 계속 유지해야하는 방송 수신기가 있습니까? 나는 앱이 죽을 때까지 작동하는 것으로 시작했다. –

+0

이 게시물을 보아라. http : //stackoverflow.com/questions/5916859/how-to-save-alarm-after-app-killing –

+0

나는 그것을 보았다. 그래프에서 서비스를 브로드 캐스트에 통합하는 방법을 알 수는 없지만 더 자세히 볼 수 있습니다. –