【发布时间】:2018-06-08 09:33:59
【问题描述】:
我已经创建了一个应用程序,我想在其中启动服务 第一次打开应用程序。当应用程序在前台时 每 5 或 10 分钟接收一次本地通知。但仅在应用程序处于活动状态或最近的应用程序时收到通知。当清除最近的应用程序时应用通知未收到。我希望此服务继续在设备上运行。
创建服务和启动服务的示例代码: 启动服务:
Intent i=new Intent(MainActivity.this,MyService.class);
startService(i);
服务:
public class MyService extends Service {
final static String TAG = MyService.class.getName();
ReceiverCall receiverCall;
private static final int NOTIFICATION_ID = 1;
MyService (){
super();
}
class Mythread implements Runnable {
int service_id;
Mythread(int service_id) {
this.service_id = service_id;
}
@Override
public void run() {
int i = 0;
synchronized (this) {
// while (i < 10) {
try {
wait(2500);
i++;
processStartNotification(String.valueOf(i));
} catch (InterruptedException e) {
e.printStackTrace();
}
// }
}
}
}
@Override
public void onCreate() {
receiverCall= new ReceiverCall();
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
super.onDestroy();
Toast.makeText(getApplicationContext(), "Service Task destroyed", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(getApplicationContext(), MyService.class);
startService(myIntent);
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Intent myIntent = new Intent(getApplicationContext(), MyService.class);
startService(myIntent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Thread thread = new Thread(new Mythread(startId));
thread.start();
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void processStartNotification(String s) {
// Do something. For example, fetch fresh data from backend to create a rich notification?
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("Scheduled Notification")
.setAutoCancel(true)
.setColor(getResources().getColor(R.color.colorAccent))
.setContentText(" Notification Service"+s)
.setSmallIcon(R.drawable.ic_launcher_background);
builder.setDeleteIntent(receiverCall.getDeleteIntent(this));
final NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, builder.build());
}
}
BroadcastReceiver Class :
public class ReceiverCall extends BroadcastReceiver {
private static final String ACTION_START_NOTIFICATION_SERVICE = "ACTION_START_NOTIFICATION_SERVICE";
private static final String ACTION_DELETE_NOTIFICATION = "ACTION_DELETE_NOTIFICATION";
private static final int NOTIFICATIONS_INTERVAL_IN_HOURS = 2;
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Service Stops", "Ohhhhhhh");
context.startService(new Intent(context, MyService.class));
}
public static PendingIntent getDeleteIntent(Context context) {
Intent intent = new Intent(context, ReceiverCall.class);
intent.setAction(ACTION_DELETE_NOTIFICATION);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}
清单声明:
<service
android:name=".MyService"
android:exported="true"
android:stopWithTask="false"></service>
<receiver android:name=".ReceiverCall">
<intent-filter>
<action android:name="com.servicesex" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
是否可以在应用程序暂停和其他任何情况下始终运行此服务。一段时间后,我的应用程序暂停,服务也暂停或停止。那么如何才能始终在后台运行此服务。
【问题讨论】:
标签: android google-maps android-fragmentactivity