我在通知栏中按通知时试图打开一个片段。我的app结构是:
当我按下通知时,活动重新打开但不在指示的片段中,并且在LogCat中,来自指示片段的数据看起来打开并加载但不是UI。
通知代码:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Comanda Noua");
mBuilder.setContentText("S-a introdus o comanda noua");
mBuilder.setTicker("Comanda noua!");
mBuilder.setSmallIcon(R.drawable.calculator_icon);
mBuilder.setAutoCancel(true);//inchide notificare dupa ce s-a dat click pe ea
//creste numar notificare
mBuilder.setNumber(++numMessages);
// cream un intent
Intent resultIntent = new Intent(this, MainActivity.class);
//am setat actiune pentru a deschide fragmentul corespunzator notificartii la apasare
resultIntent.setAction("Action1");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// notificationID allows you to update the notification later on.
mNotificationManager.notify(notificationID, mBuilder.build());
按下通知时打开片段的代码:
Intent intent = getIntent();
try{
String action = intent.getAction();
// Log.d("action:",action);
if(action.equals("Action1")){
//Log.d("deschidem agenda:",action);
AgendaFragment fragment = new AgendaFragment();
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction.replace(R.id.frame_container, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null).commit();
}else{
Log.d("eroare", "Intent was null");
}
}catch(Exception e){
Log.e("eroare", "Problem consuming action from intent", e);
}
为什么活动开放但片段没有?
尝试下面的代码,这是显示通知的更简单的方法:
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.setAction("Action1");
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);
mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon_notification, "Your title", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this, "Your title", "Your text", pendingIntent);
mNotificationManager.notify(notificationID, notification);
尝试广播
在通知类中,您必须sendBroadcast。它的默认方法。
`intent.setAction("one");
sendBroadcast(intent);`
在Base Activity中只需创建Broadcast接收器
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent!=null && intent.getAction().equals("one"))
{
displayView(fragement_position);
}
}
};
在创建BaseActivity时,重新调整广播接收器
IntentFilter filter = new IntentFilter();
filter.addAction("one");
registerReceiver(getList, filter);
您必须添加intentFilter
并且您必须取消注册基础活动的销毁
@Override
protected void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}