我已創建活動NotificationActivity,並且我想在點擊導航抽屜的item_alarm菜單後打開它。我已經這樣做了,但它不起作用。關於導航抽屜項目的打開活動(而不是片段)單擊
主要活動
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(SELECTED_TAG, selectedIndex);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
if (id == R.id.sunday){
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.fragment_container, new SundayFragment()).commit();
}
else if (id == R.id.monday){
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.fragment_container, new MondayFragment()).commit();
}
else if (id == R.id.tuesday){
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.fragment_container, new TuesdayFragment()).commit();
}
else if (id == R.id.wednesday){
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.fragment_container, new WednesdayFragment()).commit();
}
else if (id == R.id.thursday){
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.fragment_container, new ThursdayFragment()).commit();
}
else if (id == R.id.friday){
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.fragment_container, new FridayFragment()).commit();
}
else if (id == R.id.saturday){
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.fragment_container, new SaturdayFragment()).commit();
}
else if(id == R.id.item_about) {
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.fragment_container, new AboutFragment()).commit();
}
else if(id == R.id.item_alarm) {
Intent i = new Intent(MainActivity.this, NotificationActivity.class);
startActivity(i);
}
else if(id == R.id.item_course) {
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.fragment_container, new CourseFragment()).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
而且NotificationActivity是:
public class NotificationActivity extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent repeating_intent = new Intent(context,Repeating_activity.class);
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(android.R.drawable.arrow_up_float)
.setContentTitle("Title")
.setContentText("Content")
.setAutoCancel(true);
notificationManager.notify(100,builder.build());
}
}
把你所有的方法放在這裏 –
@LuizFernandoSalva terra我編輯了這個問題 –
這不是一個Activity,是一個BroadcastReceiver,你不能手動啓動BroadcastReceiver。 –