【问题标题】:Is there anyway to start android activity from the broadcast service when ever it receives to start the activity?无论如何,当它收到启动活动时,是否可以从广播服务启动 android 活动?
【发布时间】:2020-06-03 19:58:43
【问题描述】:
public class RestartService extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(new Intent(context,ApplicationStaterService.class));
        } else {
            context.startService(new Intent(context,ApplicationStaterService.class));
        }
        Log.i("Form BrsdCast","Run");

        boolean shouldStart = intent.getBooleanExtra("startAct",false);
        String url = intent.getStringExtra("urlData");
        assert url != null;
        Log.i("Form BrsdCast",url);
        if (shouldStart && !url.isEmpty()){
            Log.i("Form_BrsdCast",url);
            launchAct(url,context);
        }
    }

    private void launchAct(String url,Context context) {
        Intent intent = new Intent(context,Check.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        Toast.makeText(context, "TESTDDDDD", Toast.LENGTH_SHORT).show();
    }

}

这将在每次收到它时都会在后台运行,即使应用程序没有运行,它也应该从后台启动活动

【问题讨论】:

    标签: android android-studio broadcastreceiver android-service


    【解决方案1】:

    您必须允许在其他应用程序上显示的权限

    How to programmatically grant the "draw over other apps" permission in android?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多