今天遇到一个奇怪的bug,PendingIntent.getActivity()在小米手机上面不起作用,在别的手机没有问题,估计是小米侵犯了rom的原因。源代码这个方法介绍如下:
/** * Retrieve a PendingIntent that will start a new activity, like calling * {@link Context#startActivity(Intent) Context.startActivity(Intent)}. * Note that the activity will be started outside of the context of an * existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK * Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent. * * @param context The Context in which this PendingIntent should start * the activity. <span style="color:#ff0000;"> * @param requestCode Private request code for the sender (currently * not used).</span> * @param intent Intent of the activity to be launched. * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, * or any of the flags as supported by * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts * of the intent that can be supplied when the actual send happens. * * @return Returns an existing or new PendingIntent matching the given * parameters. May return null only if {@link #FLAG_NO_CREATE} has been * supplied. */ public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags) { String packageName = context.getPackageName(); String resolvedType = intent != null ? intent.resolveTypeIfNeeded( context.getContentResolver()) : null; try { intent.setAllowFds(false); IIntentSender target = ActivityManagerNative.getDefault().getIntentSender( IActivityManager.INTENT_SENDER_ACTIVITY, packageName, null, null, requestCode, new Intent[] { intent }, resolvedType != null ? new String[] { resolvedType } : null, flags); return target != null ? new PendingIntent(target) : null; } catch (RemoteException e) { } return null; }
标红的部分说现在这个参数不用了,但是不知这个参数究竟有什么作用(有时间了要看一下)
解决办法就是给requestCode一个随机数,示例代码如下:
Notification notif = new Notification(); notif.contentIntent = PendingIntent.getActivity(this.context, (new Random().nextInt(100)), this.intent, PendingIntent.FLAG_UPDATE_CURRENT);
如上,bug可解,原因不详,了解的大神告诉一下吧。
时间: 2024-12-16 15:44:50