Как запустить службу переднего плана из целевого API приемника вещания уровня 31 или выше: приложение закрытоAndroid

Форум для тех, кто программирует под Android
Ответить
Anonymous
 Как запустить службу переднего плана из целевого API приемника вещания уровня 31 или выше: приложение закрыто

Сообщение Anonymous »

Я пытаюсь запустить службу windowleads, которая представляет собой тип диалогового окна системного оповещения (появляется, когда приложение закрыто: как плавающее окно), но не запускает службу windowleads во время трансляции.
Когда я запускаю службу из действия тогда это работает.
После изменений в поведении в Android 13 или 14+ служба переднего плана Android может быть ограничена, поскольку несколько недель я изо всех сил пытаюсь запустить службу переднего плана через широковещательный приемник на уровне API 31 или выше. .
Журналы
Caused by: java.lang.RuntimeException: android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service

android.app.RemoteServiceException$ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground():

WindowsLeadsService
public class WindowLeadsService extends Service {
private WindowManager windowManager;
private View dialogView;
private TextView textView3;

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
dialogView = inflater.inflate(R.layout.customized_leads, null);

// Initialize MapView
textView3 = dialogView.findViewById(R.id.textview3);

// Set up the layout parameters for the window
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);

// Add the view to the window
windowManager.addView(dialogView, params);

// Initialize TextViews and LinearLayouts
TextView textView2 = dialogView.findViewById(R.id.textview2);
TextView textView3 = dialogView.findViewById(R.id.textview3);
TextView textView5 = dialogView.findViewById(R.id.textview5);
TextView textView6 = dialogView.findViewById(R.id.textview6);
TextView textView7 = dialogView.findViewById(R.id.textview7);
TextView textView8 = dialogView.findViewById(R.id.textview8);
ImageView imageView1 = dialogView.findViewById(R.id.imageview1);
ImageView imageView2 = dialogView.findViewById(R.id.imageview2);
LinearLayout linear14 = dialogView.findViewById(R.id.linear14);
LinearLayout linear28 = dialogView.findViewById(R.id.linear28);

// Set click listeners
linear14.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// Handle click
stopSelf();
String phoneNumber = "tel:+917233456780";
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(phoneNumber));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
startActivity(intent);
} catch (SecurityException excep) {
Toast.makeText(
getApplicationContext(),
"Something went wrong.",
Toast.LENGTH_SHORT)
.show();
}
}
});

linear28.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// Handle click
stopSelf();
}
});
}

@Override
public void onDestroy() {
super.onDestroy();
if (dialogView != null) {
windowManager.removeView(dialogView);
}
}
}

BroadcastReciever:
public class RebootService extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, WindowLeadsService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(serviceIntent);
} else {
context.startService(serviceIntent);
}
}
}
}


Подробнее здесь: https://stackoverflow.com/questions/788 ... l-31-or-ab
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»