好吧,我有一个带有通知的服务,我希望在用户点击通知时打开额外的新意图,所以当MainActivity打开时我可以得到额外的.我尝试过不同的方法,但我只是找不到如何让它发挥作用.
这是我尝试过的:
这是MyService
Intent intent = new Intent(MyService.this, MainActivity.class);
intent.putExtra("EXTRA", StringTEST);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent
.getActivity(getApplicationContext(),
0, intent, 0);
这打开活动很好,但我不能得到额外的?这就是我试图得到它的方式:
主要活动
@Override
public void onNewIntent(Intent intent){
Bundle extras = intent.getExtras();
if(extras != null){
String name = extras.getString("EXTRA");
System.out.println(name);
}
}
@Override
protected void onResume() {
Bundle extras = getIntent().getExtras();
System.out.println(extras+"");
if (extras != null) {
try {
onNewIntent(getIntent());
Intent i = getIntent();
String name = i.getStringExtra("EXTRA");
System.out.println(name);
currentDir = new File(name);
changeFiles();
} catch (Exception e) {
e.printStackTrace();
}
}
super.onResume();
}
这是我尝试的两种方式,但我仍然无效……我做错了什么?
谢谢