final Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final Intent notificationIntent = new Intent(context,YourActivity.class);
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
这是可行的,毫无疑问,但问题是当您将意图设置为 ACTION _ MAIN 时。那么您将无法将任何 bundle 设置为意图。我的意思是,您的原始数据将不会从目标活动接收到,因为 ACTION _ MAIN 不能包含任何额外的数据。
您可以将您的活动设置为 singleTask 并正常地调用您的意图,而不需要设置 ACTION _ MAIN 并从目标活动的 onNewInent ()方法接收意图。
public class YourRootActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (!isTaskRoot()) // checks if this root activity is at root, if not, we presented it from notification and we are resuming the app from previous open state
{
val extras = intent.extras // do stuffs with extras.
finish();
return;
}
// OtherWise start the app as usual
}
}