2013-01-19 41 views

回答

1

您可以通過註冊廣播接收器來完成此操作。這樣,您可以在點擊通知時在接收器中運行代碼,而無需顯示任何UI。

基本接收器代碼:

public class Provider extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     super.onReceive(context, intent); 
     //Your code here 

    } 
} 

在清單:

public static void showNotification(Context context) { 
    CharSequence title = "title"; 
    CharSequence message = "text"; 

    NotificationManager notificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(<image>, title, System.currentTimeMillis()); 
    notification.flags = Notification.FLAG_ONGOING_EVENT; 

    Intent notificationIntent = new Intent(context, Provider.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0); 

    notification.setLatestEventInfo(context, title, message, pendingIntent); 
    notificationManager.notify(<notification id>, notification); 
} 
:爲通知

<receiver android:name="Provider" /> 

示例代碼