Android Studio AlarmManager后台重复运行
我正在尝试创建一个后台服务,因此当应用程序关闭时,后台将运行一个服务,该服务将使用AlarmManager每隔5秒向手机显示一个通知 我目前将java服务文件设置为:Android Studio AlarmManager后台重复运行,android,android-intent,service,timer,background,Android,Android Intent,Service,Timer,Background,我正在尝试创建一个后台服务,因此当应用程序关闭时,后台将运行一个服务,该服务将使用AlarmManager每隔5秒向手机显示一个通知 我目前将java服务文件设置为: package com.racecoursedatatechnologies.rdtstockmanagement; import android.app.Service; import android.app.AlarmManager; import android.content.Intent; import android
package com.racecoursedatatechnologies.rdtstockmanagement;
import android.app.Service;
import android.app.AlarmManager;
import android.content.Intent;
import android.util.Log;
import android.os.Binder;
import android.os.IBinder;
import android.app.PendingIntent;
import android.app.PendingIntent;
public class BackIntentService extends Service {
private PendingIntent pendingIntent;
private AlarmManager manager;
public class LocalBinder extends Binder {
BackIntentService getService() {
return BackIntentService.this;
}
}
@Override
public void onCreate() {
startAlarm();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("LocalService", "Received start id " + startId + ": " + intent);
return START_NOT_STICKY;
}
public void startAlarm()
{
manager = (AlarmManager)getSystemService(this.ALARM_SERVICE);
Intent intent = new Intent(this, BackNotifyAlarm.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
int interval = 1000 * 5;
manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Started", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
// Tell the user we stopped.
Log.d("LocalService", "Stopped");
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();
/**
* Show a notification while this service is running.
*/
private void showNotification() {
//Create a notification and set it's content and display it.
NotificationManager notif=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notify=new Notification.Builder
(getApplicationContext()).setContentTitle("RDT Stock").setContentText("HELLOOO").
setContentTitle("Low Stock Items").setSmallIcon(R.drawable.noticon).build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notif.notify(11, notify);
}
}
BackNotifyAlarm广播java文件:
public class BackNotifyAlarm extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
//PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
//wl.acquire();
Toast.makeText(context, "Notification sent!", Toast.LENGTH_LONG).show();
//wl.release();
}
我正在启动
MainActivity
文件中的服务。服务启动,然后服务运行警报,但是警报似乎只运行一次,不会重复,知道为什么吗?在Android 6.0+上,您想要的东西实际上是不可能的。最接近的解决方案是使用AlarmManager
(可能与服务配套使用)或JobScheduler
(在Android 5.0+上)。不要试图让您的服务连续运行,只是为了观察时钟的滴答声。我已经用中的AlarmManager更新了主代码。但由于某些原因,它不是每5秒运行一次,似乎需要更长的时间。你不能这么快发出警报。在安卓5.1+上,最短时间为一分钟,在安卓6.0+上,在打瞌睡模式下的频率会低很多。干杯。我现在用一个线程来排序。