2015-07-13 119 views
0

我第一次使用「Broadcast Receiver」。 我的主要主題是從狀態欄讀取通知並將其顯示在列表視圖中。java.lang.RuntimeException:接收廣播錯誤意圖

但是,當我打電話廣播接收器我越來越波瀾異常和應用程序因崩潰而關閉。

07-13 14:19:49.081: E/AndroidRuntime(3985): FATAL EXCEPTION: main 
07-13 14:19:49.081: E/AndroidRuntime(3985): Process: com.kc.mobile.notifications, PID: 3985 
07-13 14:19:49.081: E/AndroidRuntime(3985): java.lang.RuntimeException: Error receiving broadcast Intent { act=NOTIFICATION_LISTENER flg=0x10 (has extras) } in c[email protected]41e9ef18 
07-13 14:19:49.081: E/AndroidRuntime(3985):  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:769) 

和源代碼是像下面::

InformationList.java

package com.kc.mobile.notifications; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.widget.ListView; 

import com.kc.mobile.notifications.adapter.NotificationAdapter; 
import com.kc.mobile.notifications.util.NotificationModel; 

public class InformationList extends Activity 
{ 
    Context mContext; 

    private NotificationReceiver nReceiver; 
    public static NotificationModel mNotification; 
    private ArrayList<NotificationModel> mNotifications = new ArrayList<NotificationModel>(); 
    ListView mList; 
    private NotificationAdapter mAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.informationlist); 
     mContext = getApplicationContext(); 

     nReceiver = new NotificationReceiver(); 
     IntentFilter filter = new IntentFilter(); 
     filter.addAction("NOTIFICATION_LISTENER"); 
     registerReceiver(nReceiver, filter); 

     Intent broadcastIntent = new Intent("NOTIFICATION_LISTENER_SERVICEE"); 
     broadcastIntent.putExtra("command", "list"); 
     sendBroadcast(broadcastIntent); 

    } 

    class NotificationReceiver extends BroadcastReceiver 
    { 

     @Override 
     public void onReceive(Context context, Intent intent) 
     { 

      byte[] byteArray = intent.getByteArrayExtra("icon"); 
      Bitmap bmp = null; 
      if (byteArray != null) 
      { 
       bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 
      } 
      String pack = intent.getStringExtra("package"); 
      String time = intent.getStringExtra("time"); 
      String title = intent.getStringExtra("title"); 
      String desc = intent.getStringExtra("desc"); 
      mNotification.setmBmp(bmp); 
      mNotification.setTitle(title); 
      mNotification.setDesc(desc); 
      mNotification.setmTime(time); 
      mNotification.setPack(pack); 

      if (mNotification != null) 
      { 
       mNotifications.add(mNotification); 
       mAdapter.notifyDataSetChanged(); 
      } 
      else 
      { 
       mNotifications = new ArrayList<NotificationModel>(); 
       mNotifications.add(mNotification); 
       mAdapter = new NotificationAdapter(getApplicationContext(), mNotifications); 
       mList = (ListView) findViewById(R.id.infolist); 
       mAdapter = new NotificationAdapter(mContext, mNotifications); 
       mList.setAdapter(mAdapter); 

      } 
     } 
    } 
} 
+0

它是否推送通知?發送url然後使用url加載圖片 – Raghunandan

+0

不推送通知。狀態欄通知(所有通知,如SMS,新聞警報,WhatsApp警報,FB警報.....) – KCRaju

+0

你還需要註銷ondestroy的活動。使用調試器,並且步驟通過看原因並修復 – Raghunandan

回答

0

檢查這個語法,你必須註冊,並同時註銷吧..

public class MyActivity extends Activity { 

     private BroadcastReceiver myBroadcastReceiver = 
      new BroadcastReceiver() { 
       @Override 
       public void onReceive(...) { 
        ... 
       } 
      }); 

     ... 

     public void onResume() { 
      super.onResume(); 
      .... 
      registerReceiver(myBroadcastReceiver, intentFilter); 
     } 

     public void onPause() { 
      super.onPause(); 
      ... 
      unregisterReceiver(myBroadcastReceiver); 
     } 
     ... 
    }