Android广播接收器无法在后台

时间:2018-03-11 19:45:55

标签: android android-studio android-broadcast android-broadcastreceiver

我有2部Android手机和不同的Android版本。第一部手机是Kitkat,第二部是Nougat。我目前正在使用以下代码。 5或7小时后,android应用程序不再检测事件。有人可以帮帮我吗?

package com.doct.patients.Broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class smsReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"You Got 1 new Sms", Toast.LENGTH_SHORT).show();
    }
}

Android AndroidManifest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.doct.patients">
    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true" />

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


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

        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait" >
        </activity>

        <receiver android:name=".Broadcast.smsReceiver">
            <intent-filter android:priority="1000">
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

您需要添加<receiver android:name="Broadcast.smsReceiver" android:permission="android.permission.BROADCAST_SMS"> <intent-filter android:priority="1000"> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> 。所以它会是这样的:

if constexpr ((!std::is_void_v<decltype(fun(x))> && ...)) {
    return std::tuple(fun(x)...);
} else {
    (fun(x), ...);
}