gpt4 book ai didi

java - 无法启动 Api 中的后台服务 > 26

转载 作者:行者123 更新时间:2023-12-01 23:15:02 25 4
gpt4 key购买 nike

尝试从后台服务启动服务,不知何故,它适用于所有低于 api 24 的 Android 版本,但实际上不适用于(oreo、Pie、..)。

但是我尝试下面的代码,以及使用 +api>24 进行测试时我遇到的问题的屏幕。

Error picture

格莱德的帮助,谢谢!!

1) 主要 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)后台服务类:

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)应用程序类:

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) list xml:

    <service
android:name=".ExampleService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:enabled="true"
android:exported="true"/>

最佳答案

您的权限在 list 中的位置。

您应该获得 FOREGROUND_SERVICE 的许可。

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

关于java - 无法启动 Api 中的后台服务 > 26,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58357035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com