2017-07-31 71 views
0

打死我有應用,其主要功能取決於報警,但報警有時候有時候解僱不,我紅一下其表示,由於系統殺死的火警之前您的應用程序,我怎麼能保證報警即使應用殺害,後在這裏要火,我怎麼設置報警如何確保火警即使應用程序通過系統

public static void setEndAlarm(){ 
    AlarmManager alarmManager = (AlarmManager) MyApplication.getContext().getSystemService(Context.ALARM_SERVICE); 
    Calendar time = Calendar.getInstance(); 
    time.setTimeInMillis(System.currentTimeMillis()); 
    time.set(Calendar.HOUR_OF_DAY, SharedPrefUtils.getEndHour(MyApplication.getContext())); 
    time.set(Calendar.MINUTE, SharedPrefUtils.getEndMin(MyApplication.getContext())); 
    time.set(Calendar.SECOND, 0); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), endPendingIntent(MyApplication.getContext())); 
} 

private static PendingIntent endPendingIntent(Context context){ 
    Intent intent = new Intent(context, ClsEndBroadcastReciever.class); 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 02, intent, PendingIntent.FLAG_ONE_SHOT); 
    return pendingIntent; 
} 

和獲得方面是這樣的:

public class MyApplication extends Application { 

private static Context mContext; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    mContext = getApplicationContext(); 
} 

public static Context getContext() { 
    return mContext; 
} 
} 

廣播接收器:

public class ClsEndBroadcastReciever extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 

    FirebaseJobDispatcher dispatcher = new 
FirebaseJobDispatcher(new GooglePlayDriver(context)); 

    dispatcher.cancel("notification"); 
    Toast.makeText(context, "End Time", Toast.LENGTH_SHORT).show(); 
} 
} 

回答

0

我不認爲你可以可靠地做到這一點沒有系統權限。

使用IntentService和START_STICKY(https://developer.android.com/reference/android/app/Service.html),以確保應用程序不會被殺掉嘗試。

+0

我覺得服務也殺死了,是真的 – blackHawk

+0

是在像OPPP一些設備,體內和許多類似的Android定製的設備,如果你從任務欄中刪除應用程序(滑動清除),然後該應用程序將強制停止,使您的所有服務被殺死。 –

+0

所以,我怎麼能保證我的應用程序 – blackHawk

0

如果您在Manifest中指定BroadcastReceiver,那麼即使您的應用程序未處於活動狀態,也會立即寫入。

清單

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 

    <!-- Activities and Services --> 

    <receiver android:name=".AlarmBroadcastReceiver"/> 

</application> 

AlarmBroadcastReceiver

public class AlarmBroadcastReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     // do what you want to do here. 
     // if the work load is heavy then launch service for it here 

    } 
} 
+0

它在清單中,問題在於有時候警報有時不會觸發 – blackHawk

+0

@blackHawk然後你可以發佈代碼告訴我們你是如何設置警報的嗎? – Marat

+0

如果應用程序沒有打開,它會在5,6天后每天發生這種警報發生它不會觸發,並且如果應用程序再次像往常一樣打開其火災 – blackHawk

相關問題