【问题标题】:Broadcast receiver not working when application close from task manager in lollipop当应用程序从棒棒糖中的任务管理器关闭时,广播接收器不起作用
【发布时间】:2017-05-07 15:37:33
【问题描述】:

我为接收短信​​创建广播接收器。它仅适用于打开的应用程序。当我们从任务管理器中删除应用程序时,它无法工作。它在果冻豆,kitkat 中工作正常,但在棒棒糖版本中不起作用。

    public class MobiricReceiver extends BroadcastReceiver {

    // Get the object of SmsManager
    final SmsManager sms = SmsManager.getDefault();

    public void onReceive(Context context, Intent intent) {

        // Retrieves a map of extended data from the intent.
        final Bundle bundle = intent.getExtras();

        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 message = currentMessage.getDisplayMessageBody();

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


                    // Show Alert
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context,
                            "senderNum: "+ senderNum + ", message: " + message, duration);
                    toast.show();

                } // end for loop
            } // bundle is null

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

        }
    }
}
Menifest中的

声明

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.admin.broadcast_demo1">
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".MobiricReceiver"
            >
            <intent-filter
                android:priority="1">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                <action android:name="android.intent.action.USER_PRESENT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

【问题讨论】:

  • 你用什么设备测试?
  • BroadcastReciever 必须在应用程序从任务管理器中关闭后工作,但此问题可能与 android.provider.Telephony.SMS_RECEIVED 操作有关。您正在测试哪种设备?
  • 我得到了解决方案。简单地把它放在活动中。 android:autoRemoveFromRecents="true" .Application 永远不会杀死。我在棒棒糖 5.1 上测试

标签: android


【解决方案1】:

这是一个非常简单的解决方案。在 Menifest--> 活动标签中添加这个

        <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:autoRemoveFromRecents="true">
        </activity>

在按下按钮后,请使用此代码完成活动

 if(android.os.Build.VERSION.SDK_INT >= 21)
                            {
                                getActivity().finishAndRemoveTask();
                            }
                            else
                            {
                                getActivity().finish();
                            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    相关资源
    最近更新 更多