2017-07-31 72 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

相关问题