有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

广播接收器包(?)中的java startActivity()

我正在尝试用BluetoothDevice启动MainActivity。ACTION\u ACL\u CONNECTED receive就像在某些堆栈溢出应答中一样

public class BTReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("BT", "Receive");
        String action = intent.getAction();
        ...
        switch (action) {
            case BluetoothDevice.ACTION_ACL_CONNECTED:
                Intent i = new Intent();
                i.setClassName("com.opendashcam", "com.opendashcam.MainActivity");
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);

        }
    }
}

像这样

Intent intent1 = new Intent(context, MainActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);

但我所能看到的只是这些日志(前两个说接收已获得,活动已通过连接到写入的MAC启动)

D/BT: Receive
D/BT: started app with 00:14:41:1E:26:27
I/Timeline: Timeline: Activity_launch_request time:129879532

我的主要活动是onCreate:

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("MA", "Started app");
        init();
    }   

共 (1) 个答案

  1. # 1 楼答案

    根据Android Developers docs,由于Android 10,Android不允许从后台启动活动:

    Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background. These restrictions help minimize interruptions for the user and keep the user more in control of what's shown on their screen.

    或者,如果单击,您可以显示将启动活动的通知:

    In nearly all cases, apps that are in the background should display time-sensitive notifications to provide urgent information to the user instead of directly starting an activity.