2013-06-21 2 views
0

아래 코드와 같이 이미 알림 표시 줄에 아이콘을 표시 할 수 있습니다. 사용자가이 아이콘을 클릭 할 때 새로운 활동 (myclass)을 시작하려고하지만이 작업을 수행하는 방법을 모르겠습니다. 의도는 어디에 두어야합니까?알림 아이콘에 대한 새 활동 시작

public class NotificationActivity extends Activity { 
    AlarmManager am; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_notification); 
     am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
     setRepeatingAlarm(); 
    } 

    public void setRepeatingAlarm() { 
     Intent intent = new Intent(this, TimeAlarm.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, 
      intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 
      (20 * 1000), pendingIntent); 
     System.out.println("Calling Alaram..."); 
    } 
} 



public class BootUpReciever extends BroadcastReceiver { 
    NotificationManager nm; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     System.out.println("in broad...."); 

     nm = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); 
     CharSequence from = "Lokesh"; 
     CharSequence message = "Notification Test..."; 
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
      new Intent(), 0); 
     Notification notif = new Notification(R.drawable.cherry_icon, 
      "Notification Test...", System.currentTimeMillis()); 
     notif.setLatestEventInfo(context, from, message, contentIntent); 
     nm.notify(1, notif); 

     if ((intent.getAction() != null) && 
       (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))) 
     { 
      System.out.println("in broadcast receiver....."); 
      Intent i = new Intent(context, MainActivity.class); 
      i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(i); 
     } 
    } 
} 
+0

당신이 매니페스트에 새로운 활동을 선언 했 :

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!"); // Creates an explicit intent for an Activity in your app Intent resultIntent = new Intent(this, ResultActivity.class); // The stack builder object will contain an artificial back stack for the // started Activity. // This ensures that navigating backward from the Activity leads out of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(ResultActivity.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); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // mId allows you to update the notification later on. mNotificationManager.notify(mId, mBuilder.build()); 

가 좋은 설명은 개발자 문서에 자세한 기사를 통해 이동? @NiceGuy – SamDroid

+0

예가 declair입니다.하지만 알림 바의 아이콘을 클릭하면 새로운 활동을 시작하기 위해이 코드에 의도를 두었습니까 ??? –

+1

질문 확인 : http://stackoverflow.com/questions/10184351/how-to-start-activity-when-user-clicks-a-notification –

답변

1

알림 빌더에 부여한 보류중인 의도에는 사용자가 알림을 클릭 할 때 활동을 시작하는 의도가 포함되어야합니다.

Intent resultIntent = new Intent(this, ResultActivity.class); 

그런 다음 당신이 pendingIntent이 의도를 사용합니다 :

PendingIntent resultPendingIntent = 
     stackBuilder.getPendingIntent(
      0, 
      PendingIntent.FLAG_UPDATE_CURRENT 
     ); 

을하고 당신이 그것을 사용하여 알림을 만들 당신은 당신의 활동 중 하나를 실행하는 의도를 만들어야합니다. 사용자가 알림을 클릭하면 ResultActivity이 실행됩니다.

전체 코드 : https://developer.android.com/guide/topics/ui/notifiers/notifications.html

+0

멋진 솔루션이지만, 두 개 이상있는 경우에만 작동합니다. 액티비티, 하나만있는 경우이 인스턴스는 singleInstance입니다. 백그라운드에있는 경우에만 액티비티를 포 그라운드로 가져 오는 알림은 화면에 아무 것도 표시되지 않습니다. – uiltonsantos

관련 문제