2011-11-01 36 views
5

Tôi biết, có rất nhiều những điều này ở đây, nhưng tôi đã thử các giải pháp cả ngày và chưa nhận được bất kỳ đâu. Không phải ví dụ về tài liệu của Google, cũng như bất kỳ cách nào trong số 5 cách khác mà tôi đã tìm thấy ở đây đều có hiệu quả đối với tôi. Như trường hợp điển hình, khi tôi nhấp vào thông báo, nó sẽ đóng thanh trạng thái và không có gì mới được hiển thị trên màn hình. Tôi đang tạo thông báo từ một dịch vụ và cần thông báo để kích hoạt hoạt động mới chưa được tạo. Tôi cũng sẽ cần một cách để chuyển thông tin cho hoạt động đó thông qua mục đích.khởi chạy hoạt động từ dịch vụ khi thông báo được nhấp vào

Và có ... đây là java dành cho Android

Sau đây là phần còn lại bị hỏng của mã của tôi.

package com.bobbb.hwk2; 

import java.io.FileOutputStream; 

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.app.Service; 
import android.content.ContentResolver; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.IBinder; 
import android.provider.ContactsContract; 
import android.widget.Toast; 

public class contactBackup extends Service 
{ 
    private NotificationManager nManager; 
    private static final int NOTIFY_ID = 1100; 

    @Override 
    public void onCreate() 
    { 
     super.onCreate(); 

     String ns = Context.NOTIFICATION_SERVICE; 
     nManager = (NotificationManager)getSystemService(ns); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     super.onStart(intent, startId); 

     // inform user that service has started 
     Toast.makeText(getApplicationContext(), R.string.service_started,Toast.LENGTH_LONG).show(); 

     String data = lookUpContacts(); 
     if(saveToSDCard(getResources().getString(R.string.backup_file_name),data)) 
     { 
      Context context = getApplicationContext(); 

      // create the statusbar notification 
      Intent nIntent = new Intent(this,contactViewer.class);//Intent nIntent = new Intent(Intent.ACTION_MAIN); 
      nIntent.setClass(context,contactViewer.class); 
      //nIntent.putExtra("data",data); 


      Notification msg = new Notification(R.drawable.icon,"All contacts records have been written to the file.",System.currentTimeMillis()); 
      // start notification 
      //PendingIntent pIntent = PendingIntent.getService(getApplicationContext(),0,nIntent,PendingIntent.FLAG_UPDATE_CURRENT|Intent.FLAG_FROM_BACKGROUND); 
      PendingIntent pIntent = PendingIntent.getActivity(this,0,nIntent,0); 
      msg.flags = Notification.FLAG_AUTO_CANCEL; 
      msg.setLatestEventInfo(context, 
            "success", 
            "All contacts records have been written to the file.", 
            pIntent); 
      nManager.notify(NOTIFY_ID,msg); 


     } 



    } 

    @Override 
    public void onDestroy() 
    { 
     nManager.cancel(NOTIFY_ID); 
     super.onDestroy(); 
    } 

    @Override 
    public IBinder onBind(Intent intent) 
    { 
     return null; 
    } 

    // function returns string containing information 
    // from contacts 
    public String lookUpContacts() 
    { 
     ... 
    } 

    public boolean saveToSDCard(String fileName, String data) 
    { 
     ... 
    } 

} 

Tôi chỉ có thể hy vọng rằng bất cứ điều gì đang gây ra vấn đề của tôi là một cái gì đó khắc phục và không nhiều trục trặc điên tôi đã nhận với nhật thực toàn phần (mà không ai khác dường như đã từng gặp>: U)

Nếu bạn có thể giúp tôi giải quyết vấn đề này, vui lòng chia sẻ. Nếu bạn không thể giúp đỡ với vấn đề cụ thể này nhưng cảm thấy bắt buộc phải nói những điều không liên quan về gửi bài, phong cách, chủ đề hoặc thực hành tốt, sau đó KHÔNG

Cảm ơn bạn: D

Trả lời

6

Edit:

bạn sẽ cần phải thêm một cờ cho FLAG_ACTIVITY_NEW_TASK:

nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

này là bởi vì bạn đang tung ra từ outsi từ ứng dụng của bạn (từ thanh thông báo hệ thống).

+0

tốt , đó không thực sự là cách bạn sử dụng putExtra ... Nhưng dựa trên lời khuyên của bạn, tôi đã thử addi ng rằng cả cờ tới Intent và PendingIntent và không tạo ra bất kỳ sự khác biệt nào. Cảm ơn mặc dù. – user980058

+0

chỉnh sửa của bạn khớp với những gì tôi đã làm. vẫn không có súc sắc: 3 – user980058

0

Đây là những gì sẽ xảy ra khi mọi người làm việc quá sức. XD Lý do duy nhất không có hướng dẫn nào tôi mệt mỏi là vì tôi đã viết sai tên hoạt động của mình trong tệp kê khai. Cảm ơn đã dừng lại do

0

Chỉ cần thêm sau trong contactBackup (lớp dịch vụ),

Intent nIntent = new Intent(this,contactViewer.class);//Intent nIntent = new Intent(Intent.ACTION_MAIN); 
     nIntent.setClass(context,contactViewer.class); 
     nIntent.putExtra("data",data); 

    nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     Notification msg = new Notification(R.drawable.icon,"All contacts records have been written to the file.",System.currentTimeMillis()); 
     // start notification 
     //PendingIntent pIntent = PendingIntent.getService(getApplicationContext(),0,nIntent,PendingIntent.FLAG_UPDATE_CURRENT|Intent.FLAG_FROM_BACKGROUND); 
     PendingIntent pIntent = PendingIntent.getActivity(this,0,nIntent,0); 
     msg.flags = Notification.FLAG_AUTO_CANCEL; 
     msg.setLatestEventInfo(context, 
           "success", 
           "All contacts records have been written to the file.", 
           pIntent); 
     nManager.notify(NOTIFY_ID,msg); 

sau đó nhận được giá trị trong lớp contactViewer,

như,

String s=getIntent().getStringExtra("data"); 
Các vấn đề liên quan