我从测试服务和通知的虚拟项目中复制了一段代码,在其中重新订购字体非常有效。
这是通知代码(与教程中的代码完全相同)
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentTitle("Request awaiting")
.setContentText("There is a service request awaiting for Your reaction");
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);//stackBuilder.getPendingIntent(0, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
nBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = nBuilder.build();
mNotificationManager.notify(1, notification);
list 看起来像:
<application android:allowBackup="true" android:icon="@drawable/ld"
android:label="@string/app_name" android:theme="@style/AppTheme">
<activity
android:name="com.iraasta.cloudcab.driver.MainActivity"
android:launchMode="singleTop"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
它在我的其他应用程序中看起来完全一样,它完美地将应用程序带到前面,就像从主屏幕中选择时一样。 但是在这个中,它每次都调用 OnCreate 并丢失整个状态。
最佳答案
要将您的应用程序以任何状态置于前台(如果它在后台),或启动您的应用程序(如果它尚未运行),您需要启动您的“根 Activity ”( ACTION=MAIN 和 CATEGORY=LAUNCHER) 并设置标志 Intent.FLAG_ACTIVITY_NEW_TASK
。这将在新任务中启动 Activity(如果您的应用尚未运行),或将现有任务置于前台(如果您的应用已在运行)。
由于您是通过通知执行此操作,因此您应该在传递给通知的 Intent
中设置 Intent.FLAG_ACTIVITY_NEW_TASK
。但是,如果您忘记这样做,Android 会为您设置此标志。
我不确定为什么设置 Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
会导致您的问题,但我可以想象这会以某种方式混淆 Android。
关于Android 通知重新排序到前面不起作用。从它工作过的APP复制过来的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27429567/