java.lang.RuntimeException:接收广播Intent

时间:2015-07-13 05:32:31

标签: android broadcastreceiver

我第一次使用“广播接收器”。 我的主题是从状态栏中读取通知并将其显示在列表视图中。

但是当我打电话给广播接收器时,我得到了轰鸣声异常,因此应用程序因崩溃而关闭。

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 com.kc.mobile.notifications.InformationList$NotificationReceiver@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);

            }
        }
    }
}

1 个答案:

答案 0 :(得分: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);
        }
        ...
    }