[英]How to start service API 28+
我有個問題。 如何啟動關閉應用程序時不會終止的服務? 我正在嘗試提供通知服務,我需要它在后台運行,並且在我想要之前不會結束。 對不起我的英語不好。我正在使用 android 工作室。
我找到了伙計們。 不過謝謝。
主要活動.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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.