Android 为什么服务在启动完成时启动,而不是在电池电量低时启动?
为什么我的服务在开机时启动但电池电量不足?代码: MyScheduleReceiver.javaAndroid 为什么服务在启动完成时启动,而不是在电池电量低时启动?,android,service,broadcastreceiver,android-notifications,bootcompleted,Android,Service,Broadcastreceiver,Android Notifications,Bootcompleted,为什么我的服务在开机时启动但电池电量不足?代码: MyScheduleReceiver.java public class MyScheduleReceiver extends BroadcastReceiver { // Restart service every 30 min private static final long REPEAT_TIME = 30*1000*4;//1800000 ; @Override public void onRece
public class MyScheduleReceiver extends BroadcastReceiver {
// Restart service every 30 min
private static final long REPEAT_TIME = 30*1000*4;//1800000 ;
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, service.class);
context.startService(service);
}}
service.java
public class service extends Service {
NotificationManager mNotificationManager;
@Override public IBinder onBind(Intent intent) {
// Not used
return null;
}
@Override public void onCreate() {
super.onCreate();
mNotificationManager= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
checkPref();
}
@Override
public void onDestroy() {
super.onDestroy();
}
private void checkPref(){
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
service.this);
notificationBuilder.setContentTitle("Title");
notificationBuilder.setContentText("Context");
notificationBuilder.setTicker("TickerText");
notificationBuilder.setWhen(System.currentTimeMillis());
notificationBuilder.setSmallIcon(R.drawable.ic_stat_icon);
Intent notificationIntent = new Intent(this, service.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notificationBuilder.setContentIntent(contentIntent);
notificationBuilder.setDefaults(Notification.DEFAULT_SOUND
| Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
mNotificationManager.notify(1,
notificationBuilder.build());
} }
并表明
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
<uses-permission android:name="android.permission.BATTERY_STATS">
<receiver android:name="MyScheduleReceiver" >
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW" />
</intent-filter>
</receiver>
<service android:name="service" >
</service>
我想在电池电量不足时显示的通知没有出现。。如果我在BOOT_已完成的情况下更改操作并重新启动设备,那么设备将正常工作。为什么?请查看以下答案:
看起来你也需要对这些意图进行编程。顺便说一句,看起来你不需要电池统计许可就可以这么做
希望能有所帮助好的,谢谢。。稍后我将尝试:-)。因此,启动完成后,不需要以编程方式编写意图。。这很奇怪:-)你认为时间是一样的吗?不知道,最好是测试;)