无法在 Api > 26 中启动后台服务
Can't Start Background Services in Api > 26
尝试从后台服务启动服务,不知何故它适用于所有低于 api 24 的 android 版本,但实际上不适用于 (oreo,Pie,..)。
但是我尝试了下面的代码,如果使用 +api>24....
进行测试,我会遇到这个问题
Error picture
非常感谢您的帮助,谢谢!!
1) Main Activity(启动服务):
public void startService(View v) {
String input = editTextInput.getText().toString();
Intent serviceIntent = new Intent(this, ExampleService.class);
serviceIntent.putExtra("inputExtra", input);
ContextCompat.startForegroundService(this, serviceIntent);
Toast.makeText(Options.this,"Notification, On.", Toast.LENGTH_LONG).show();
}
public void stopService(View v) {
Intent serviceIntent = new Intent(this, ExampleService.class);
stopService(serviceIntent);
Toast.makeText(Options.this,"Notification, Off.", Toast.LENGTH_LONG).show();
}
2) 后台服务 Class :
package com.demo.testttt;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import static com.demo.testttt.App.CHANNEL_ID;
public class ExampleService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String input = intent.getStringExtra("inputExtra");
Intent notificationIntent = new Intent(this, Home.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Example Service")
.setContentText(input)
.setSmallIcon(R.drawable.logo)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
//do heavy work on a background thread
//stopSelf();
}
else {
String input = intent.getStringExtra("inputExtra");
Intent notificationIntent = new Intent(this, Home.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
Notification notification = new Notification.Builder(ExampleService.this)
.setVibrate(new long[] { 350, 350})
.setContentTitle("Example Service")
.setContentText(input)
.setSmallIcon(R.drawable.logo)
.setContentIntent(pendingIntent)
.build();
NotificationManager notifmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
/// notifmanager.notify(0,notification);
startForeground(1, notification);
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
3) 应用 Class :
package package com.demo.testttt;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
public class App extends Application {
public static final String CHANNEL_ID = "exampleServiceChannel";
@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Example Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
}
4) 清单 xml :
<service
android:name=".ExampleService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:enabled="true"
android:exported="true"/>
Manifest 中您的许可在哪里。
您应该得到 FOREGROUND_SERVICE.
的许可
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
尝试从后台服务启动服务,不知何故它适用于所有低于 api 24 的 android 版本,但实际上不适用于 (oreo,Pie,..)。
但是我尝试了下面的代码,如果使用 +api>24....
进行测试,我会遇到这个问题Error picture
非常感谢您的帮助,谢谢!!
1) Main Activity(启动服务):
public void startService(View v) {
String input = editTextInput.getText().toString();
Intent serviceIntent = new Intent(this, ExampleService.class);
serviceIntent.putExtra("inputExtra", input);
ContextCompat.startForegroundService(this, serviceIntent);
Toast.makeText(Options.this,"Notification, On.", Toast.LENGTH_LONG).show();
}
public void stopService(View v) {
Intent serviceIntent = new Intent(this, ExampleService.class);
stopService(serviceIntent);
Toast.makeText(Options.this,"Notification, Off.", Toast.LENGTH_LONG).show();
}
2) 后台服务 Class :
package com.demo.testttt;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import static com.demo.testttt.App.CHANNEL_ID;
public class ExampleService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String input = intent.getStringExtra("inputExtra");
Intent notificationIntent = new Intent(this, Home.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Example Service")
.setContentText(input)
.setSmallIcon(R.drawable.logo)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
//do heavy work on a background thread
//stopSelf();
}
else {
String input = intent.getStringExtra("inputExtra");
Intent notificationIntent = new Intent(this, Home.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
Notification notification = new Notification.Builder(ExampleService.this)
.setVibrate(new long[] { 350, 350})
.setContentTitle("Example Service")
.setContentText(input)
.setSmallIcon(R.drawable.logo)
.setContentIntent(pendingIntent)
.build();
NotificationManager notifmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
/// notifmanager.notify(0,notification);
startForeground(1, notification);
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
3) 应用 Class :
package package com.demo.testttt;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
public class App extends Application {
public static final String CHANNEL_ID = "exampleServiceChannel";
@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Example Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
}
4) 清单 xml :
<service
android:name=".ExampleService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:enabled="true"
android:exported="true"/>
Manifest 中您的许可在哪里。
您应该得到 FOREGROUND_SERVICE.
的许可<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />