jon*_*lee 15 android android-virtual-device
我使用 android 10 [android Q,galaxy 10],
我使用 android studio 3.3,
使用 AVD,并制作了 api 29 [android 10] 虚拟电话。
在虚拟机上,我执行我的应用程序,然后,我启动其他应用程序,如日历、计算器。所以我的应用程序活动进入后台模式。
当我在 BroadcastReceiver 收到一条消息时。我调用 startActivity。
在这里,代码 --> 公共类 myReceiver 扩展了 BroadcastReceiver {}
public void onReceive(Context context, Intent intent)
{
Intent intentRun = new Intent(context, LoginSuccess.class);
intentRun.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intentRun);
}
Run Code Online (Sandbox Code Playgroud)
但 LoginSuccess 活动没有出现。[当我的应用程序处于后台模式时]
使用相同的代码,当我的应用程序处于前台模式时,LoginSuccess 活动显示得很好。
上图显示了我在广播接收器中调用 startActivity 之前的调用堆栈。
我已阅读有关 android 10 后台活动问题的指南。[developer.android.com/~~某个位置]
在引导线上,
我开始知道如果 Activity 存在于调用堆栈中,它甚至可以在后台模式下启动。
上面的代码,它尝试启动最近调用堆栈中存在的活动。
为什么 startActivity 调用在后台模式下失败?[也许不会失败,但无论如何没有激活到前台]
Ere*_*kçi 19
使用 Android Q,如果您的应用不包含以下链接中列出的那些例外,则无法自动从后台启动 Activity。
https://developer.android.com/guide/components/activities/background-starts
可能的解决方案:
1-您可以选择只显示服务通知,然后单击开始挂起的意图
2-您可以使用全屏意图立即显示您的意图,如另一个答案所示并由谷歌建议。
对于全屏意图解决方案,如官方文档所述
当用户正在使用设备时,系统 UI 可能会选择显示提醒通知,而不是启动此意图。
3-要在后台自动启动活动,我认为最可能的解决方案是将“SYSTEM_ALERT_WINDOW”添加到清单文件中。并在应用程序第一次打开时请求用户许可。(用户可以手动授予此权限-(设置-应用程序-您的应用程序-高级-绘制其他应用程序))
请求权限的示例代码:
在清单中:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Run Code Online (Sandbox Code Playgroud)
在应用程序的某个地方:
public static int ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE= 2323;
//if the user already granted the permission or the API is below Android 10 no need to ask for permission
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
!Settings.canDrawOverlays(getContext()))
{RequestPermission()}
private void RequestPermission() {
// Check if Android M or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Show alert dialog to the user saying a separate permission is needed
// Launch the settings activity if the user prefers
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getActivity().getPackageName()));
startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(getContext())) {
PermissionDenied();
}
else
{
// Permission Granted-System will work
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Kau*_*hal 10
我使用以下逻辑打开活动。作为谷歌,博客说如果你想在后台服务中打开活动以在 android 10 或更高版本上使用通知。
在清单中:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Run Code Online (Sandbox Code Playgroud)
例子:
private void startActivity() {
Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.siren);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
AudioAttributes attributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
String CHANNEL_ID = BuildConfig.APPLICATION_ID.concat("_notification_id");
String CHANNEL_NAME = BuildConfig.APPLICATION_ID.concat("_notification_name");
assert notificationManager != null;
NotificationChannel mChannel = notificationManager.getNotificationChannel(CHANNEL_ID);
if (mChannel == null) {
mChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
mChannel.setSound(sound, attributes);
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setSmallIcon(R.drawable.logo)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.login))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setFullScreenIntent(openScreen(Constants.NOTIFICATION_ID), true)
.setAutoCancel(true)
.setOngoing(true);
Notification notification = builder.build();
notificationManager.notify(Constants.NOTIFICATION_ID, notification);
} else {
startActivity(new Intent(BackgroundService.this, LoginActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
}
private PendingIntent openScreen(int notificationId) {
Intent fullScreenIntent = new Intent(this, LoginActivity.class);
fullScreenIntent.putExtra(Constants.NOTIFICATION_IDS, notificationId);
return PendingIntent.getActivity(this, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18680 次 |
| 最近记录: |