从堆栈中删除应用程序时如何保持服务正常运行 [英] How to keep service alive when remove app from stack
问题描述
我想使用Alarm Manager每分钟显示通知,我已经实现了下面的代码,它可以正常工作,但是问题是当我从堆栈中删除应用程序时服务未运行。
我想保持生命,我在onStartCommand中尝试了 START_STICKY ,还使用了 onTaskRemoved ,但这是相同的。
<我也尝试使用WakefulIntentService来实现,但是问题是相同的。我的代码如下。
在MainActivity中
Intent myIntent = new Intent(NotificationDemo.this,MyReceiver.class);
myIntent.putExtra( title, 2分钟);
Random random = new Random();
int m = random.nextInt(9999-1000)+ 1000;
Log.d( m :::通知中,m +);
myIntent.putExtra( id,m);
endingIntent = PendingIntent.getBroadcast(NotificationDemo.this,m,myIntent,0);
AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);
// AlarmManager.setRepeating(AlarmManager.RTC,System.currentTimeMillis(),alarmManager.InteingIntent);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(),
1 * 60 * 1000,
endingIntent);
MyService
公共类MyAlarmService扩展服务{
private NotificationManager mManager;
@Override
public IBinder onBind(Intent arg0){
// TODO自动生成的方法存根
返回null;
}
@Override
public void onCreate(){
// TODO自动生成的方法存根
super.onCreate();
}
@Override
public int onStartCommand(Intent intent,int flags,int startId){
字符串标题= intent.getStringExtra( title );
int id = intent.getIntExtra( id,0);
mManager =(NotificationManager)this.getApplicationContext()。getSystemService(this.getApplicationContext()。NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
通知通知=新通知(R.mipmap.ic_launcher,标题,System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Log.d( id ::,id +);
PendingIntentendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(),id,intent1,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags | = Notification.FLAG_AUTO_CANCEL;
// // notification.setLatestEventInfo(this.getApplicationContext(), AlarmManagerDemo,这是一条测试消息!,endingNotificationIntent);
NotificationCompat.Builder builder =新的NotificationCompat.Builder(MyAlarmService.this);
通知= builder.setContentTitle(title)
.setContentText( Hellooo ...)
.setTicker( Good Evening ...)
。 setSmallIcon(android.R.drawable.ic_btn_speak_now)
.setVibrate(new long [] {1000,1000,1000,100})
.setLights(5,5,5)
.setContentIntent (pendingNotificationIntent).build();
mManager.notify(id,通知);
startForeground(1337,通知);
返回START_STICKY;
}
@Override
public void onDestroy(){
// TODO自动生成的方法存根
super.onDestroy();
// sendBroadcast(new Intent( IWillStartAuto));
}
@Override
public void onTaskRemoved(Intent rootIntent){
super.onTaskRemoved(rootIntent);
// sendBroadcast(new Intent( IWillStartAuto));
// Intent intent = new Intent(getApplicationContext(),MyReceiver.class);
// sendBroadcast(intent);
}
}
这是我的接收者
公共类MyReceiver扩展了BroadcastReceiver {
@Override
public void onReceive(Context context,Intent intent){
字符串标题= intent.getStringExtra( title);
int id = intent.getIntExtra( id,0);
Intent service1 = new Intent(context,MyAlarmService.class);
service1.putExtra( title,title);
service1.putExtra( id,id);
context.startService(service1);
}
}
在清单中
< receiver android:name =。MyReceiver>
<!-<意图过滤器>
< action android:name = IWillStartAuto />
< / intent-filter>-
<意图过滤器>
< action android:name = android.intent.action.BOOT_COMPLETED />
< / intent-filter>
< / receiver>
< service
android:name =。MyAlarmService
android:enabled = true
android:stopWithTask = false />
您需要在前台启动服务。
您的服务OnCreate()方法中缺少此内容。
startForeground(1337,notification);
返回START_STICKY;
不要在活动或onDestroy()方法的片段中停止服务。
I want to show notification every minute using Alarm Manager, I have implemented below code, it's working fine but the problem is when I remove app from stack the service is not running.
I want keep alive, I have tried START_STICKY in onStartCommand and also used onTaskRemoved but it's same.
I also tried to implement using WakefulIntentService but the problem is same. My code is below.
In MainActivity
Intent myIntent = new Intent(NotificationDemo.this, MyReceiver.class);
myIntent.putExtra("title", "2 minutes");
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
Log.d("m::: In Notification", m + "");
myIntent.putExtra("id", m);
pendingIntent = PendingIntent.getBroadcast(NotificationDemo.this, m, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),alarmManager.Inte pendingIntent);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(),
1 * 60 * 1000,
pendingIntent);
MyService
public class MyAlarmService extends Service {
private NotificationManager mManager;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String title = intent.getStringExtra("title");
int id = intent.getIntExtra("id", 0);
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class);
Notification notification = new Notification(R.mipmap.ic_launcher, title, System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Log.d("id::", id + "");
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), id, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MyAlarmService.this);
notification = builder.setContentTitle(title)
.setContentText("Hellooo...")
.setTicker("Good Evening...")
.setSmallIcon(android.R.drawable.ic_btn_speak_now)
.setVibrate(new long[]{1000, 1000, 1000, 100})
.setLights(5, 5, 5)
.setContentIntent(pendingNotificationIntent).build();
mManager.notify(id, notification);
startForeground(1337, notification);
return START_STICKY;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// sendBroadcast(new Intent("IWillStartAuto"));
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
// sendBroadcast(new Intent("IWillStartAuto"));
// Intent intent = new Intent(getApplicationContext(),MyReceiver.class);
// sendBroadcast(intent);
}
}
and this is my receiver
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String title = intent.getStringExtra("title");
int id = intent.getIntExtra("id", 0);
Intent service1 = new Intent(context, MyAlarmService.class);
service1.putExtra("title", title);
service1.putExtra("id", id);
context.startService(service1);
}
}
In Manifest
<receiver android:name=".MyReceiver">
<!--<intent-filter>
<action android:name="IWillStartAuto"/>
</intent-filter>-->
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".MyAlarmService"
android:enabled="true"
android:stopWithTask="false" />
You need to start your service in foreground.
You are missing something this in your service OnCreate() method.
startForeground(1337, notification);
return START_STICKY;
And don't stop your service in your activities or fragments onDestroy() methods.
这篇关于从堆栈中删除应用程序时如何保持服务正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!