2013-09-01 35 views
8

Tôi có vấn đề MainActivity của tôi có thể được tạo theo 3 cách: 1) khởi động standart App 2) từ Dịch vụ 3) từ nhấp chuột thông báo. Làm cách nào để kiểm tra thời điểm bắt đầu từ lần nhấp thông báo?Cách kiểm tra xem Hoạt động có bắt đầu từ thông báo

mã thông báo:

private void createNotification() 
{ 
    Log.d("service createNotification",MainActivity.TAG); 
    Context context = getApplicationContext(); 
    Intent notificationIntent = new Intent(this,MainActivity.class); 
    intent.putExtra(AppNames.IS_NOTIFICATION_INTENT,true); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
      notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
      .setContentTitle(this.getString(R.string.notification_title)) 
      .setContentText(this.getString(R.string.notification_text))    
      .setContentIntent(pendingIntent)    
      .setSmallIcon(R.drawable.ic_launcher); 

    getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(AppNames.APP_NOTIFICATION, builder.getNotification()); 
} 

Trả lời

8

thêm

intent.putExtra("started_from","notification"); 

vào mã bắt đầu mục đích từ các thông báo, và điều tương tự với startActivity khác các cuộc gọi chỉ cần thay đổi giá trị, sau đó bên trong bạn hoạt động

String startedFrom = getIntent().getStringExtra("started_from"); 

để tham khảo thêm câu hỏi này: How do I get extra data from intent on Android?

+2

unfochnately phương thức này không hoạt động, Luôn nhận được thông báo trong cả 3 trường hợp. – Abbath

+0

bạn cần thay đổi "thông báo" trong putExtra thành một thứ khác, thành "dịch vụ" chẳng hạn. –

+0

không hoạt động, hãy xem @http: //stackoverflow.com/questions/14122085/determine-if-activity-is-called-by-a-notification nếu any1 quan tâm –

Các vấn đề liên quan