初学安卓,我想实现一个功能,就是比如说qq,微信等聊天软件,长按home键把软件从后台中移除,但是有消息发来时依然可以向通知栏发送通知
这是我写的代码,这个退出界面可以正常后台每10秒发送一个通知,但是从后台结束后就不不发送了,是什么原因?是因为后台结束软件时顺便把timer也结束的原因吗?我应该如何写可以把软件从后台中移除,但是依然可以向通知栏十秒发一个通知,小弟在此先谢过了
public int onStartCommand(final Intent intent, int flags, int startId) {
long period = 10000; //
int delay=intent.getIntExtra("delayTime",0);
if (null == timer ) {
timer = new Timer();
}
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
NotificationManager mn= (NotificationManager) PushService.this.getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(PushService.this);
Intent notificationIntent = new Intent(PushService.this,MainActivity.class);//
PendingIntent contentIntent = PendingIntent.getActivity(PushService.this,0,notificationIntent,0);
builder.setContentIntent(contentIntent);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setTicker(intent.getStringExtra("tickerText")); //
builder.setContentText(intent.getStringExtra("contentText")); //
builder.setContentTitle(intent.getStringExtra("contentTitle"));//
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.build();
mn.notify((int)System.currentTimeMillis(),notification);
}
},delay, period);
return START_STICKY_COMPATIBILITY;
//return super.onStartCommand(intent, flags, startId);
}
这是我写的代码,这个退出界面可以正常后台每10秒发送一个通知,但是从后台结束后就不不发送了,是什么原因?是因为后台结束软件时顺便把timer也结束的原因吗?我应该如何写可以把软件从后台中移除,但是依然可以向通知栏十秒发一个通知,小弟在此先谢过了
public int onStartCommand(final Intent intent, int flags, int startId) {
long period = 10000; //
int delay=intent.getIntExtra("delayTime",0);
if (null == timer ) {
timer = new Timer();
}
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
NotificationManager mn= (NotificationManager) PushService.this.getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(PushService.this);
Intent notificationIntent = new Intent(PushService.this,MainActivity.class);//
PendingIntent contentIntent = PendingIntent.getActivity(PushService.this,0,notificationIntent,0);
builder.setContentIntent(contentIntent);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setTicker(intent.getStringExtra("tickerText")); //
builder.setContentText(intent.getStringExtra("contentText")); //
builder.setContentTitle(intent.getStringExtra("contentTitle"));//
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.build();
mn.notify((int)System.currentTimeMillis(),notification);
}
},delay, period);
return START_STICKY_COMPATIBILITY;
//return super.onStartCommand(intent, flags, startId);
}