我的应用程序也能正常运行,但是12或24小时后无法运行,因为该程序已被操作系统关闭。
如何防止操作系统关闭程序?
我的手机型号:Huawei G620S-L02
我这样设置闹钟:
AlarmManager alarmManager = (AlarmManager) this.mContext.getSystemService(Context.ALARM_SERVICE);
Intent intentAlarm = new Intent(this.mContext, AlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.mContext, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
long time = System.currentTimeMillis() + 25 * 60 * 60 * 1000
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
这是AlarmReciever
:
public class AlarmReciever extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
PendingIntent pi = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher) // notification icon
.setTicker(sticker)
.setContentTitle("title") // title for notification
.setContentText("text) // message for notification
.setAutoCancel(true) // clear notification after click
.setContentIntent(pi)
.setLights(0xff00ff00, 300, 500)
.setShowWhen(false);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
我的Manifest
是:
<receiver
android:name=".AlarmReciever"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
答案 0 :(得分:0)
在某些设备(如HUAWEI)中,您的应用必须在受保护的应用中进行检查:
转到设置=&gt;受保护的应用=&gt;检查了您的应用