我有个问题。 怎么才能在关闭APP的时候启动一个不会杀人的服务呢? 我正在尝试使一个通知服务,我需要它是在后台生活,不会结束,直到我想。 对不起,我的英语不好。我正在使用android Studio。
我找到了伙计们。 不过还是谢谢你。
主要活动。java
getApplicationContext().startForegroundService(new Intent(dis, foregroundService.class));
前台服务。java
public class foregroundService extends Service {
private Handler handler = new Handler();
@Nullable
@Override
public IBinder onBind(Intent intent) {
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
return null;
}
private void start (){
handler.postDelayed(runnable, 6000);
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
Toast.makeText(foregroundService.this, "Refresh", Toast.LENGTH_SHORT).show();
start();
}
};
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onCreate() {
super.onCreate();
// dont show the notification//
startForeground(1,new Notification.Builder(this).build());
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
start();
}
}