昨晚我将操作系统版本更新为android 10,从那时起,广播接收器中的startActivity函数什么都不做。这是我根据CommonsWare的答案尝试开始活动的方法:
Intent i = new Intent(context, AlarmNotificationActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // This is at least android 10...
Log.d("Debug", "This is android 10");
// Start the alert via full-screen intent.
PendingIntent startAlarmPendingIntent = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
String CHANNEL_ID = "my_channel_02";
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
context.getString(R.string.notification_channel_name_second),
NotificationManager.IMPORTANCE_HIGH);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle("Um, hi!")
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setFullScreenIntent(startAlarmPendingIntent, true);
Log.d("Debug", "Try to load screen");
notificationManager.notify(0, builder.build());
}
日志显示我正在进入notify命令,但是什么也没有发生。我正在请求清单上的USE_FULL_SCREEN_INTENT权限,因此我应该能够使用全屏意图。 由于这个问题,我的应用现在无用了。有人知道如何解决吗?
答案 0 :(得分:5)
大约10个月前,宣布了Android 10开始后台活动的限制。您可以在the documentation中详细了解它。
使用具有相关联的全屏Intent
的高优先级通知。参见the documentation。 This sample app通过使用WorkManager
触发需要提醒用户的后台事件来演示这一点。在那里,我使用高优先级通知,而不是直接启动活动:
val pi = PendingIntent.getActivity(
appContext,
0,
Intent(appContext, MainActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT
)
val builder = NotificationCompat.Builder(appContext, CHANNEL_WHATEVER)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Um, hi!")
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setFullScreenIntent(pi, true)
val mgr = appContext.getSystemService(NotificationManager::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& mgr.getNotificationChannel(CHANNEL_WHATEVER) == null
) {
mgr.createNotificationChannel(
NotificationChannel(
CHANNEL_WHATEVER,
"Whatever",
NotificationManager.IMPORTANCE_HIGH
)
)
}
mgr.notify(NOTIF_ID, builder.build())
答案 1 :(得分:1)
大约10个月前,宣布了Android 10开始后台活动的限制。您可以在documentation中详细了解它。
因此,您需要有一个高级通知,当用户单击通知时,您的活动将被打开通知
x <- c("7-2014" , "1-2018", "11-2014", "7-2014", "1-2018", "1-2018")
答案 2 :(得分:1)
您可以使用SYSTEM_ALERT_WINDOW强制在android 10中启动活动窗口,请参考此设置superposition setting:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".OnBootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
在已启动的应用程序中检查权限:
private void RequestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + this.getPackageName()));
startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
} else {
//Permission Granted-System will work
}
}
}
您可以将用户意图设为android较早版本
public class OnBootReceiver extends BroadcastReceiver {
private static final String TAG = OnBootReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
try {
Intent activity = new Intent(context, MainActivity.class);
activity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activity);
} catch (Exception e){
Log.d(TAG,e.getMessage()+"");
}
}
}