我有一個android應用程序需要定期生成通知,當我們點擊通知時,通知中顯示的消息將顯示在原始應用程序的活動內的TextView中。Android Broadcast Reciever打開意圖
爲此,我創建了一個AlarmReceiver.java類。
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
Log.d("ME", "Notification started");
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_noticon)
.setContentTitle("Notification")
.setContentText("You have to open app to see this message in detail");
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
Intent resultIntent = new Intent("com.app.AlarmBack");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(AlarmBack.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_ONE_SHOT);
mBuilder.setContentIntent(resultPendingIntent);
}
也是我創建的類名爲AlarmBack.java
public class AlarmBack extends MainActivity {
TextView tv_alert_back_display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.alert_back, frameLayout);
mDrawerList.setItemChecked(position, true);
setTitle(mTitle);
tv_alert_back_display = (TextView) findViewById(R.id.tv_alert_back_display);
tv_alert_back_display.setText("This is the message that needs your attension");
}}
的意圖,但問題是,這是行不通的。它並沒有打開它想要打開的意圖。所以,有人可以幫助解決我的問題。
要添加這個方法當通知被點擊或者你想改變t時打開你的活動他的通知的標題和內容? –
@ZubairAkber我想在點擊通知時打開活動 –
你有沒有試過我的答案它適用於我希望所以它也會爲你工作 –