|
android Studio中,如何在后台调用Activity (后台打开新的Activity,前台仍显示原有的Activity)
在Android中实现后台调用Activity而不切换到前台的方法根据Android版本不同,有以下两种推荐实现方式:
1、Android 10+推荐方案:使用通知代替直接启动Activity(符合后台限制政策)
2、兼容方案:在目标Activity中使用moveTaskToBack()方法
以下是两种实现方式的完整代码:
MainActivity.java
- package com.example.backgroundactivity;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.os.Bundle;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.app.NotificationCompat;
- import androidx.core.app.NotificationManagerCompat;
- public class MainActivity extends AppCompatActivity {
- private static final String CHANNEL_ID = "background_activity_channel";
- private static final int NOTIFICATION_ID = 1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- createNotificationChannel();
- findViewById(R.id.btn_start).setOnClickListener(v -> {
- // 创建后台Activity的PendingIntent
- Intent intent = new Intent(this, BackgroundActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- PendingIntent pendingIntent = PendingIntent.getActivity(
- this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
- // 构建通知
- NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
- .setSmallIcon(R.drawable.ic_notification)
- .setContentTitle("后台Activity已启动")
- .setContentText("点击查看详情")
- .setPriority(NotificationCompat.PRIORITY_HIGH)
- .setFullScreenIntent(pendingIntent, true)
- .setAutoCancel(true);
- // 发送通知
- NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
- notificationManager.notify(NOTIFICATION_ID, builder.build());
- });
- }
- private void createNotificationChannel() {
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
- CharSequence name = "Background Activity Channel";
- String description = "Channel for background activity notifications";
- int importance = android.app.NotificationManager.IMPORTANCE_HIGH;
- android.app.NotificationChannel channel = new android.app.NotificationChannel(CHANNEL_ID, name, importance);
- channel.setDescription(description);
-
- android.app.NotificationManager notificationManager = getSystemService(android.app.NotificationManager.class);
- notificationManager.createNotificationChannel(channel);
- }
- }
- }
复制代码 BackgroundActivity.java:
- package com.example.backgroundactivity;
- import android.os.Bundle;
- import androidx.appcompat.app.AppCompatActivity;
- public class BackgroundActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_background);
- }
- }
复制代码 AndroidMenifest.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.backgroundactivity">
- <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
- <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".BackgroundActivity"
- android:excludeFromRecents="true"
- android:showOnLockScreen="true"/>
- </application>
- </manifest>
复制代码 关键点说明:
1、moveTaskToBack(true)方法会将整个任务栈移到后台
2、isTaskRoot()检查当前Activity是否是任务栈根节点
3、需要添加GET_TASKS权限来检查应用前后台状态
4、Android 10+建议改用通知方式,此方案可能被系统限制
注意事项:
1、在Android 10及以上版本,后台启动Activity会受到系统限制
2、对于时效性要求高的场景,建议改用通知+FullScreenIntent方式
3、此方案在用户切换回应用时会显示最后启动的Activity
|
|