private void initNotification(){
CharSequence tikerText = getResources().getString(R.string.tickerText);
NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,tikerText,System.currentTimeMillis());
notification.flags = Notification.FLAG_ONGOING_EVENT;
Context context = getApplicationContext();
CharSequence contentTitle = getResources().getString(R.string.contentTitle);
CharSequence contentText = getResources().getString(R.string.contentText);
Intent notifIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
notifIntent.putExtra("showAudioFrag",true);
PendingIntent contentIntent = PendingIntent.getActivity(context,0,notifIntent,0);
notification.setLatestEventInfo(context,contentTitle,contentText,contentIntent);
mNotificationManager.notify(NOTIFICATION_ID,notification);
}
在MainActivity的OnCreate方法中,我添加了下面的代码:
intent = getIntent();
boolean reloadFragmentFromNotification = intent.getBooleanExtra("showAudioFrag",false);
if (reloadFragmentFromNotification){
Fragment fragment = new TaleActivity_Audio();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container,fragment)
.commit();
} else {
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
如果需要完整的项目,请访问 GitHub:https://github.com/radric/Tales_updated.git。
Fragment fragment = new Fragment(); FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.container,fragment) .commit();- Stan Malcolm