-2

我已創建活動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()); 





    } 
} 
+1

把你所有的方法放在這裏 –

+0

@LuizFernandoSalva terra我編輯了這個問題 –

+0

這不是一個Activity,是一個BroadcastReceiver,你不能手動啓動BroadcastReceiver。 –

回答

0

如果你想打開菜單項點擊裏面的Activity,你必須改變你的類是Activity

public class NotificationActivity extends AppCompatActivity implements View.OnclickListener { 

    //Create a button instance 
    private Button btn; 

    @Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.your_layout_here); 
    btn = (Button)findViewById(R.id.btn); //you have to put a button on your layout your_layour.xml 
    btn.setOnClickListener(this); 
    } 


    //when your button is pressed, this method will be triggered. 
     @Override 
    public void onClick(View view){ 
     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()); 

    } 
} 
+0

讓我說清楚。我想打開一個活動菜單項點擊並顯示通知,當它裏面的按鈕被按下。 –

+0

我編輯我的答案 –