我試圖通過通知和事件運行一個活動onCreate
我想「重定向」。爲此,在Intent
類中添加信息。一個重要的特性是生成通知的類通過服務來執行。我從getApplicationContext
方法檢索android.app.Application
類提供的方法。每當我撥打方法getExtras()
正在返回null
。我究竟做錯了什麼?Intent.getExtras()總是返回空
public class OXAppUpdateHandler {
private void addNotification(Context context, int iconID,
CharSequence tickerText, CharSequence title, CharSequence content) {
CharSequence notificationTicket = tickerText;
CharSequence notificationTitle = title;
CharSequence notificationContent = content;
long when = System.currentTimeMillis();
Intent intent = new Intent(context, MainActivity_.class);
intent.setFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra(OPEN_UPDATE_ACTIVITY_KEY, 1);
PendingIntent pendingIntent =
PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(
Context.NOTIFICATION_SERVICE);
Notification notification =
new Notification(iconID, notificationTicket, when);
notification.setLatestEventInfo(context, notificationTitle,
notificationContent, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
}
public static boolean isUpdateStart(Intent intent) {
Bundle bundle = intent.getExtras();
boolean result = bundle != null &&
bundle.containsKey(OPEN_UPDATE_ACTIVITY_KEY);
if (result) {
bundle.remove(OPEN_UPDATE_ACTIVITY_KEY);
}
return result;
}
}
@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (OXAppUpdateHandler.isUpdateStart(getIntent())) {
startActivity(new Intent(this, UpdateActivity_.class));
}
}
}
你可以發佈代碼,意圖額外的? –
嘗試獲取基本上下文我.e進入通知廣播或其他地方。 –
@ChangdeoJadhav方法OXAppUpdateHandler。 addNotification(...)通過代碼intent.putExtra(OPEN_UPDATE_ACTIVITY_KEY,1); – adrianosepe