我正在重构一些代码,以便我的应用程序每天在给定时间从网站提取数据.从我的研究来看,这似乎AlarmManager是最合适的方法.
我一直关注的教程是:http://mobile.tutsplus.com/tutorials/android/android-fundamentals-downloading-data-with-services/
到目前为止,AlarmManager并BroadcastReceiver似乎工作,然而Service似乎从来没有开始(即onStartCommand似乎没有被调用)
以下是我到目前为止的代码的重要摘要:
MyActivity.java
private void setRecurringAlarm(Context context) {
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getDefault());
updateTime.set(Calendar.HOUR_OF_DAY, 20);
updateTime.set(Calendar.MINUTE, 30);
Intent downloader = new Intent(context, AlarmReceiver.class);
downloader.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, downloader, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// should be AlarmManager.INTERVAL_DAY (but changed to 15min for testing)
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
Log.d("MyActivity", "Set alarmManager.setRepeating to: " + updateTime.getTime().toLocaleString());
}
Run Code Online (Sandbox Code Playgroud)
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver { …Run Code Online (Sandbox Code Playgroud) android broadcastreceiver alarmmanager android-pendingintent
我可以Intent从一个PendingIntent?
这是场景:
Intent(让我们称之为myIntent)myInfo)PendingIntent(myPendingIntent)使用myIntentAlarmManager和设置闹钟myPendingIntentPendingIntent.getBroadcastmyInfo的myIntent是在myPendingIntent这可能吗?通过浏览谷歌,我接近结论,这是不可能的.
我想知道是否有可能从我自己没有创建的PendingIntent中获取更多信息.更确切地说:是否有可能以某种方式检索原始Intent的PendingIntent?我不需要执行它,但想打印它的内容.
查看PendingIntent它的代码显示了一个隐藏的方法:
/** @hide */
public IIntentSender getTarget() {
return mTarget;
}
Run Code Online (Sandbox Code Playgroud)
然而,这IIntentSender也是隐藏的,与BinderIPC(我猜)相关的东西有关.不那么容易.有任何想法吗?
我想用来AlarmManager.AlarmClockInfo设置闹钟.
这个构造函数需要时间和PendingIntent文档中描述的时间和:
可用于显示或编辑闹钟细节的意图.
然后setAlarmClock( )还接受一个待定的意图,在文档中描述为:
警报响起时执行的操作
据我所知,使用的PendingIntent通过setAlarmClock( ),然而,又是怎样PendingIntent使用的AlarmClockInfo?如何用它来编辑闹钟的细节?
我昨晚将操作系统版本更新为 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 …Run Code Online (Sandbox Code Playgroud) android broadcastreceiver android-pendingintent start-activity android-10.0
我尝试使用下一个代码为广播接收器启动活动
Intent i = new Intent(context, AlarmNotification.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...
NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (mgr.getNotificationChannel(CHANNEL_WHATEVER)==null) {
mgr.createNotificationChannel(new NotificationChannel(CHANNEL_WHATEVER,
"Whatever", NotificationManager.IMPORTANCE_HIGH));
}
mgr.notify(NOTIFY_ID, buildNormal(context, i).build());
}
private NotificationCompat.Builder buildNormal(Context context, Intent intent) {
NotificationCompat.Builder b=
new NotificationCompat.Builder(context, CHANNEL_WHATEVER);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(TEXT)
.setContentText(TEXT)
.setFullScreenIntent(buildPendingIntent(context, intent), true);
return(b);
}
private PendingIntent buildPendingIntent(Context context, Intent intent) {
return(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
}
Run Code Online (Sandbox Code Playgroud)
一开始,一切正常。但是如果我进入应用程序设置,关闭CHANNEL_WHATEVER的通知通道,然后再打开它。稍后当我调用 NotificationManager.notify 时,它会在通知抽屉中显示通知,但不会启动活动。如果我删除该应用程序并重新安装,它又可以正常工作了。这是我应该报告的 android 10 …
android broadcastreceiver android-intent android-pendingintent android-10.0
使用带有待处理广播的警报,这一直按预期工作,直到最近在 Android 10 上运行的 RealMe 设备和在 Android 7.1.1 上运行的少数OPPO 设备开始出现崩溃
碰撞 :
IllegalStateException: Maximum limit of concurrent alarms 500 reached for uid on AlarmManager.setRepeating()
堆栈跟踪 :
Caused by java.lang.IllegalStateException: Maximum limit of concurrent alarms 500 reached for uid: u0a336, callingPackage: com.example.your.app
at android.os.Parcel.createException + 2095(Parcel.java:2095)
at android.os.Parcel.readException + 2055(Parcel.java:2055)
at android.os.Parcel.readException + 2003(Parcel.java:2003)
at android.app.IAlarmManager$Stub$Proxy.set + 320(IAlarmManager.java:320)
at android.app.AlarmManager.setImpl + 709(AlarmManager.java:709)
at android.app.AlarmManager.setRepeating + 454(AlarmManager.java:454)
Run Code Online (Sandbox Code Playgroud)
通过使用设置闹钟:
val pendingIntent = PendingIntent.getBroadcast(
applicationContext, 0,
Intent(applicationContext, MyBroadcastReceiver::class.java), …Run Code Online (Sandbox Code Playgroud) 与这个问题类似,但不一样
更新到 Android 12 (SDK 31) 后,我们更改PendingIntent.getActivity(context, 0, intent, 0)为PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)建议的。
但PendingIntent.FLAG_IMMUTABLE不适用于 23 以下的 SDK。如果我添加if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)以保留两个版本,我会保留有关未使用正确标志的 lint 警告else。
这里的预期行为是什么?谢谢!
我\xc2\xb4已经开始测试我的应用程序在Android 12上的问题,并且收到了一些关于为主屏幕小部件设置的待处理意图的可变性标志的警告。这些可变性标志现在从 SDK 31 开始是强制性的。在 31 之前,此代码可以工作,但会出现 lint 警告:
\nval myIntent = Intent(context, MainActivity::class.java)\nval myPendingIntent = PendingIntent.getActivity(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT )\nmyRemoteViews.setOnClickPendingIntent(R.id.widget_button, myPendingIntent)\nRun Code Online (Sandbox Code Playgroud)\n在 Android 12 上,需要添加第二个标志,如下所示:
\nval myIntent = Intent(context, MainActivity::class.java)\nval myPendingIntent = PendingIntent.getActivity(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)\nmyRemoteViews.setOnClickPendingIntent(R.id.widget_button, myPendingIntent)\nRun Code Online (Sandbox Code Playgroud)\n到目前为止,一切都很好。但是,对于某些小部件操作,不仅需要导航到应用程序,还需要导航到特定片段并添加片段参数。对于这些情况,我\xc2\xb4一直在使用 NavDeepLinkBuilder 创建pendingIntent\n https://developer.android.google.cn/reference/kotlin/androidx/navigation/NavDeepLinkBuilder
\nval myPendingIntent = NavDeepLinkBuilder(context)\n .setGraph(R.navigation.nav_main)\n .setDestination(R.id.myFragment)\n .setArguments(\n myFragment.bundle(myArgs)\n )\n .createPendingIntent()\nmyRemoteViews.setOnClickPendingIntent(R.id.widget_button, myPendingIntent)\nRun Code Online (Sandbox Code Playgroud)\n在您将构建目标设置为 sdk 31 并在 Android 12(模拟器)上构建之前,此方法非常有效。在这种情况下,它会在使用以下堆栈跟踪创建小部件时崩溃:
\n 2021-11-03 17:41:27.792 20309-20309/com.package.debug E/AndroidRuntime: FATAL EXCEPTION: main\n Process: com.package.debug, PID: 20309\n java.lang.RuntimeException: …Run Code Online (Sandbox Code Playgroud) android android-pendingintent android-architecture-navigation android-deep-link android-12
请帮我解决这个问题。我给出了 git 的链接。
https://github.com/Vasajj/radio_tysa_fm.git
PendingIntent 出现问题,包:
在 android 8 上一切都很好
Installing build\app\outputs\flutter-apk\app.apk...
Debug service listening on ws://127.0.0.1:55138/iMdUmbZTaNc=/ws
Syncing files to device sdk gphone64 x86 64...
I/javaClass(18284): Calling to method: initService
I/javaClass(18284): start service invoked
I/javaClass(18284): Attempting to initialize service...
I/javaClass(18284): Service not bound, binding now....
I/javaClass(18284): Mapping method call to player item object
I/javaClass(18284): Firing up service. (onStartCommand)...
I/javaClass(18284): LocalBroadCastManager Received...
W/e.radio_tysa_f(18284): Accessing hidden method Landroid/media/AudioTrack;->getLatency()I (unsupported, reflection, allowed)
I/ExoPlayerImpl(18284): Init 398a550 [ExoPlayerLib/2.13.1] [emulator64_x86_64_arm64, sdk_gphone64_x86_64, Google, 32]
I/javaClass(18284): Pushing Event: …Run Code Online (Sandbox Code Playgroud) android ×10
android-10.0 ×3
alarmmanager ×2
android-12 ×2
android-architecture-navigation ×1
exoplayer2.x ×1
flutter ×1