当前位置: 首页 > 知识库问答 >
问题:

Android广播意图

邢心水
2023-03-14

当我收到一条消息,我正在用谷歌服务从我的桌面发送到我的Android系统时,我正在用谷歌服务进行广播,在广播后,我的应用程序崩溃了,我的日志被切断了

     W/dalvikvm(27503): threadid=1: thread exiting with uncaught exception (group=0x418e7ba8)
      FATAL EXCEPTION: main
      Process: com.avakoo.service, PID: 27503
      java.lang.RuntimeException: Error receiving broadcast Intent { act=com.avakoo.service.DISPLAY_MESSAGE flg=0x10 (has extras) } in com.avakoo.service.MainActivity$1@4262fb78
        at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:769)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
      Caused by: java.lang.NullPointerException
        at com.avakoo.service.MainActivity$1.onReceive(MainActivity.java:226)
        at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:759)
        ... 9 more
     I/Process(27503): Sending signal. PID: 27503 SIG: 9

我的GCM服务类公共类GCMIntentService扩展了GCMBaseIntentService{

        private static final String TAG = "GCMIntentService";
        private static final int NOTIFICATION_ID = 001;

        public GCMIntentService() {
            super(SENDER_ID);
        }

        /**
         * Method called on device registered
         **/
        @Override
        protected void onRegistered(Context context, String registrationId) {
            Log.i(TAG, "Device registered: regId = " + registrationId);
            displayMessage(context, "Your device registred with GCM");
            Log.d("PASS", RegisterActivity.password);
            ServerUtilities.register(context, RegisterActivity.password, RegisterActivity.email, registrationId);
        }

        /**
         * Method called on device un registred
         * */
        @Override
        protected void onUnregistered(Context context, String registrationId) {
            Log.i(TAG, "Device unregistered");
            displayMessage(context, getString(R.string.gcm_unregistered));
            ServerUtilities.unregister(context, registrationId);
        }

        /**
         * Method called on Receiving a new message
         * */
        @Override
        protected void onMessage(Context context, Intent intent) {
            Log.i(TAG, "Received message");
            //String message = intent.getExtras().getString("message");
            CharSequence[] message1 = intent.getCharSequenceArrayExtra("message");
            String message = " A V A K O O";//intent.getExtras().getString("message");

            displayMessage(context, message);
            // notifies user
            generateNotification(context, message);
        }

        /**
         * Method called on receiving a deleted message
         * */
        @Override
        protected void onDeletedMessages(Context context, int total) {
            Log.i(TAG, "Received deleted messages notification");
            String message = getString(R.string.gcm_deleted, total);
            displayMessage(context, message);
            // notifies user
            generateNotification(context, message);
        }

        /**
         * Method called on Error
         * */
        @Override
        public void onError(Context context, String errorId) {
            Log.i(TAG, "Received error: " + errorId);
            displayMessage(context, getString(R.string.gcm_error, errorId));
        }

        @Override
        protected boolean onRecoverableError(Context context, String errorId) {
            // log message
            Log.i(TAG, "Received recoverable error: " + errorId);
            displayMessage(context, getString(R.string.gcm_recoverable_error,
                    errorId));
            return super.onRecoverableError(context, errorId);
        }

        /**
         * Issues a notification to inform the user that server has sent a message.
         */
        private static void generateNotification(Context context, String message) {

            int icon = R.drawable.ic_launcher;
            long when = System.currentTimeMillis();
            NotificationManager notificationManager = (NotificationManager)
                    context.getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = new Notification(icon, message, when);

            String title = message;


            Intent notificationIntent = new Intent(context, MyNotification.class);

           /* Intent waIntent = new Intent(Intent.ACTION_SEND);
            waIntent.setType("text/plain");

            waIntent.setPackage("com.whatsapp");
            if (waIntent != null) {
                notificationIntent.setData(Uri.parse("text"));
                waIntent.setFlags(waIntent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(Intent.createChooser(waIntent, "Share with"));
            } */

            notificationIntent.setData(Uri.parse(message));


            // set intent so it does not start a new activity
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
            notification.setLatestEventInfo(context, title, message, intent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;

         // Play default notification sound
            notification.defaults |= Notification.DEFAULT_SOUND;


            // Vibrate if vibrate is enabled
            notification.defaults |= Notification.DEFAULT_VIBRATE;


            notificationManager.notify(0, notification);


        }


     }

我的显示消息函数公共静态无效displayMessage(上下文,字符串消息){Intent意图=new Intent(DISPLAY_MESSAGE_ACTION);intent.putExtra(EXTRA_MESSAGE,消息);context.sendBroadcast(意图);}

共有1个答案

孔鸿哲
2023-03-14

您需要为Pending帐篷设置此标志:Pending帐篷。标记\u取消\u当前。

PendingIntent.getActivity(this, _ID,
                navigationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

使用此标志,您的意图消息在接收器上不会为空

 类似资料:
  • DALVIK线程:(互斥:TLL=0 TSL=0 TSCL=0 GHL=0) 后台进程prio=5 tid=15 WAIT group=“main”scount=1 dscount=0 obj=0x4216e7e0 self=0x400b6a18 systid=24414 nice=0 sched=0/0 cgrp=apps handle=1074723664 state=s schedstat=

  • 本文向大家介绍Android 广播的分类?相关面试题,主要包含被问及Android 广播的分类?时的应答技巧和注意事项,需要的朋友参考一下 分为有序广播和无序广播两类。 无序广播发送代码: 无序广播的监听代码: 有序广播发送:   有序接收:

  • 我正在编写一个用于usb主机通信的android应用程序。当按下按钮时,我正在发送来自android的样本数据。当我点击按钮时,它显示一个错误,称为错误接收广播意图。请告诉我如何解决这个问题。这是我的usb驱动程序活动: 这是我的usb处理器: 这是我的主要活动: 这是我的日志:

  • 广播(Broadcast)机制用于进程/线程间通信,广播分为广播发送和广播接收两个过程,其中广播接收者BroadcastReceiver便是Android四大组件之一。 BroadcastReceiver分为两类: 静态广播接收者:通过AndroidManifest.xml的标签来申明的BroadcastReceiver。 动态广播接收者:通过AMS.registerReceiver()方式注册的

  • 输出如下: 如果两个数组的维数不相同,则元素到元素的操作是不可能的。 然而,在 NumPy 中仍然可以对形状不相似的数组进行操作,因为它拥有广播功能。 较小的数组会广播到较大数组的大小,以便使它们的形状可兼容。 如果满足以下规则,可以进行广播: 如果输入在每个维度中的大小与输出大小匹配,或其值正好为 1,则在计算中可它。 如果上述规则产生有效结果,并且满足以下条件之一,那么数组被称为可广播的。 数

  • 原文:Broadcasting 另见:numpy.broadcast 术语广播描述了NumPy在算术运算时如何处理不同形状的数组。 在某些条件下,较小的数组“广播”成较大的数组以便有相同的形状。 广播提供了一种矢量化操作数组的方法,这样可以在C而不是Python中进行循环。 它可以在不制作不必要的数据副本的情况下实现这一点,并且通常可以实现高效 然而,有些情况下广播是一个坏主意,因为它会导致内存使