2011-08-25 41 views
12

Tôi đang sử dụng đoạn mã sau để khởi động thông báo khi một dịch vụ được bắt đầu Via AlarmManager:Làm cách nào để đặt trình xử lý nhấp chuột để thông báo?

nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 
CharSequence from = "App"; 
CharSequence message = "Getting Latest Info..."; 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0); 
Notification notif = new Notification(R.drawable.icon, 
    "Getting Latest Info...", System.currentTimeMillis()); 
notif.setLatestEventInfo(this, from, message, contentIntent); 
nm.notify(1, notif); 

Làm thế nào để thiết lập một ý định cho mặt hàng này để khi người dùng nhấp vào nó, nó sẽ khởi động một số Hoạt động?

Trả lời

12

Về cơ bản, bạn cần đặt lớp Hoạt động như một phần ý định của bạn vào PendingIntent. Hiện tại ý định của bạn trống. Để chuyển hướng đến hoạt động mới, nó phải là:

// This line of yours should contain the activity that you want to launch. 
// You are currently just passing empty new Intent() 
PendingIntent contentIntent = 
    PendingIntent.getActivity(this, 0, new Intent(this, MyActivity.class), 0); 

+0

Bạn có thể cung cấp ví dụ không? – yoshi24

+0

Tôi đã thêm mã có liên quan mà bạn cần phải sửa đổi – momo

+0

Ngoài ra tôi biết điều này bên ngoài câu hỏi, là có anyway tôi có thể thiết lập một Extra để được thông qua với nó? – yoshi24

23

Đối với bình luận yoshi24, bạn có thể thiết lập tính năng bổ sung như thế này.

final Intent intent = new Intent(this, MyActivity.class); 
intent.setData(data); 
intent.putExtra("key", "value"); 
final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); 

Bạn cần phải nhận thức được điều này là tốt trước khi đi cho cấp phát intents

https://stackoverflow.com/questions/1198558/how-to-send-parameters-from-a-notification-click-to-an-activity

CẬP NHẬT một số điều như thế này sẽ làm việc cho bạn

int mainfest bạn

<activity android:name=".MyActivity" android:launchMode="singleTop" ... /> 

trong hoạt động của bạn

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    processIntent(getIntent()); 
} 

@Override 
protected void onNewIntent(Intent intent) {  
    processIntent(intent); 
}; 

private void processIntent(Intent intent){ 
    //get your extras 
} 
+0

Liên kết bạn đăng là gì, tôi đang cố gắng làm gì? – yoshi24

+1

giống như bạn đã đăng – Samuel

+1

bạn phải thay đổi cờ ý định là ** android: launchMode = "singleTask" ** trong tệp kê khai và ghi đè ** onNewIntent (Intent intent) ** bên trong hoạt động. điều này sẽ cho phép bạn nhận các tham số. – Samuel

6

tôi đã làm nó,

  • tôi thêm Intent.FLAG_ACTIVITY_CLEAR_TOP đến mới ý định

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    Notification notification = new Notification(R.drawable.ic_launcher, 
         "A new notification", System.currentTimeMillis()); 
    // Hide the notification after its selected 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    
    Intent intent = new Intent(this, NoficationDemoActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    Bundle bundle = new Bundle(); 
    bundle.putString("buzz", "buzz"); 
    intent.putExtras(bundle); 
    PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0); 
    notification.setLatestEventInfo(this, "This is the title", 
         "This is the text", activity); 
    notification.number += 1; 
    notificationManager.notify(0, notification); 
    
  • onCreate tôi làm như sau:

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    if(getIntent().getExtras()!=null){ 
        Toast.makeText(this, "Click", Toast.LENGTH_SHORT).show(); 
    } 
    
Các vấn đề liên quan