从任务管理器关闭应用程序后,广播接收器在棒棒糖中不起作用 [英] Broadcast receiver is not working in lollipop after application closed from task manager

查看:25
本文介绍了从任务管理器关闭应用程序后,广播接收器在棒棒糖中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是广播接收器.此代码适用于 kitkat 和 jeallyBean.在此我们简单地接收传入消息的通知.当任何消息进入移动设备时,它会显示通知.

This is the Broadcastreceiver. This code is working in the kitkat and jeallyBean. In this we simple receive the notification of incoming message. It shows notification when any message come in mobile.

    public class IncomingSms extends BroadcastReceiver {
    final SmsManager sms = SmsManager.getDefault();
    Notification myNotication;
    NotificationManager nm;
    public void onReceive(Context context, Intent intent) {
        nm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        final Bundle bundle = intent.getExtras();
        Intent startServiceIntent = new Intent(context, MainActivity.class);

        try {

            if (bundle != null) {

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    String senderNum = phoneNumber;
                    String compnum=phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();

                    Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);



                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
                    toast.show();
                    if(senderNum.equals(compnum))
                    {

                        Toast.makeText(context,"Come in the loop",Toast.LENGTH_LONG).show();
                        Intent intent2 = new Intent(context,MainActivity.class);

                        PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent2, 0);

                        Notification.Builder builder = new Notification.Builder(context);

                        builder.setAutoCancel(true);
                        builder.setTicker("this is ticker text");
                        builder.setContentTitle("WhatsApp Notification");
                        builder.setContentText("You have a new message");
                        builder.setSmallIcon(R.drawable.pretty_bullet);
                        builder.setContentIntent(pendingIntent);
                        builder.setOngoing(true);
                        builder.setSubText("This is subtext...");   
                        builder.setNumber(100);
                        builder.build();

                        myNotication = builder.getNotification();
                        nm.notify(50, myNotication);
                    }
                } 
            } 

        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" +e);

        }
    }  
}

在清单中我声明

<uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS"/>

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="ANDROID.PERMISSION.WRITE_SETTINGS"/>
    <receiver android:name=".IncomingSms">
            <intent-filter
                android:priority="1">
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
    </receiver>

从任务管理器关闭应用程序时,这不起作用.但它适用于 jallybean.我想显示应用程序是打开还是关闭的通知.

This is not working when application is closed from the task manager. But it works on the jallybean. I want to show the notification whether application is open or closed.

推荐答案

请看以下内容,

Android 广播接收器未在应用程序关闭时执行

我还认为您可能想了解如何设置优先级.请阅读以下内容,

I also think that you might want to read up on how to set the priority. Please read the following,

AndroidManifestIntentFilter_priority

int AndroidManifestIntentFilter_priority 指定相对处理特定意图的重要性或能力.对于接收器,这控制了它们的执行顺序以接收广播(注意对于异步广播,这个顺序是忽略).对于活动,这提供了有关活动效果如何的信息活动正在处理一个意图;当多个活动匹配一个意图并有不同的优先级,只有那些具有更高优先级值将被视为匹配.

int AndroidManifestIntentFilter_priority Specify the relative importance or ability in handling a particular Intent. For receivers, this controls the order in which they are executed to receive a broadcast (note that for asynchronous broadcasts, this order is ignored). For activities, this provides information about how good an activity is handling an Intent; when multiple activities match an intent and have different priorities, only those with the higher priority value will be considered a match.

仅当您确实需要强加某些特定顺序时才使用收到广播,或想强行将活动置于永远比别人更受欢迎.该值是一个整数,具有数字越大越好.

Only use if you really need to impose some specific order in which the broadcasts are received, or want to forcibly place an activity to always be preferred over others. The value is a single integer, with higher numbers considered to be better.

必须是整数值,例如100".

Must be an integer value, such as "100".

这也可能是对资源的引用(形式为"@[package:]type:name") 或主题属性(在表单中"?[package:][type:]name") 包含此类型的值.

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

这个对应全局属性资源符号优先级.

This corresponds to the global attribute resource symbol priority.

常量值:2 (0x00000002)

Constant Value: 2 (0x00000002)

这篇关于从任务管理器关闭应用程序后,广播接收器在棒棒糖中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆