广播接收器未接收到SMS消息

时间:2019-07-09 04:54:23

标签: android service broadcastreceiver

我正在尝试编写广播接收器以处理接收到的SMS,并启动警报对话框以接收来自用户的输入。当关闭主应用程序(MainActivity)(通过轻扫)时,此广播接收器有望工作。

基于用户的输入,接收器在通过警报请求用户许可后,将一些内容添加到android文件系统中的文件中。我注意到BroadcastReceiver的onReceive根本没有被调用

  1. 我正在使用MainActivity中的requestPermissions从用户那里获取权限
  2. 我正在启动名为ServiceCommunicator的服务
  3. ServiceCommunicator注册接收者以收听SMS
  4. 收件人显示警报消息

我也尝试过没有ServiceCommunicator(接收者已在MainActivity本身中注册)

在以上两种情况下,都不会触发接收者的onCreate。接收器在adb shell命令中列出:

adb shell cmd程序包查询接收器--brief -a android.provider.Telephony.SMS_RECEIVED

MainActivity具有以下作为onCreate方法的一部分:


int PERMISSION_ALL = 1;
String[] PERMISSIONS = {
        android.Manifest.permission.RECEIVE_SMS,
        android.Manifest.permission.SEND_SMS,
};

ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);

Intent serviceIntent = new Intent(this, ServiceCommunicator.class);
startService(serviceIntent);

...

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
    {
        switch(requestCode)
        {
            case MY_PERMISSIONS_REQUEST_RECEIVE_SMS:
            {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                {
                    Toast.makeText(this, "Thanks for SMS receive permissions", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(this, "SMS can't be received", Toast.LENGTH_LONG).show();
                }
                break;
            }
            case MY_PERMISSIONS_REQUEST_SEND_SMS:
            {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    Toast.makeText(getApplicationContext(), "Thanks for SMS send permissions",
                            Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(),
                            "SMS can't be sent", Toast.LENGTH_LONG).show();
                }
                break;
            }
        }
        return;
    }

ServiceCommunicator具有以下内容:

receiverIntentFilter = new IntentFilter();
receiverIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
myReceiver = new MyReceiver();

// Register SMS event receiver

registerReceiver(myReceiver, receiverIntentFilter);

receiver在onReceive中具有以下内容:


if (intent.getAction() == "android.provider.Telephony.SMS_RECEIVED")
{

...

Intent alertDialogIntent = new Intent(context, AlertDialogActivity.class);
                        alertDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
alertDialogIntent.putExtra("ALERT_MSG", alertMsg);
alertDialogIntent.putExtra("MESSAGE", "\n" + msg);
context.startActivity(alertDialogIntent);

}

AlertDialogActivity具有以下内容:


AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder
        .setTitle("Add information to system?")
        .setMessage(alertMsg)
...

Manifest.xml具有以下内容。更新了文件(对不起,之前已经错过了):

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

        <receiver android:priority="2147483647" android:name=".MyReceiver" android:enabled="true" android:exported="true">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED">

                </action>
            </intent-filter>
        </receiver>
        <service android:process="com.example.rtien.ServiceCommunicator" android:enabled="true" android:exported="true" android:stopWithTask="false" android:name="com.example.rtien.ServiceCommunicator" />
        <activity android:name=".MainActivity" android:theme="@style/Theme.AppCompat">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

预期结果:关闭MainActivity时,接收器应在后台运行。接收器中的onReceive方法应被触发。应该显示警报消息,并将数据添加到文件中

观察到的结果:当MainActivity处于活动状态时,将触发接收器的onReceive,但在其关闭时则不会触发

1 个答案:

答案 0 :(得分:1)

发生这种情况的原因是,当您从“最近”托盘中滑动应用程序时,它会在许多设备中完全关闭该应用程序,从而导致服务也关闭,并取消了广播接收器的注册。

请尝试使用此代码,以避免取消服务,并确保您具有自动启动权限和所有其他权限

将其添加到您的服务中

@Override
    public void onTaskRemoved(Intent rootIntent) {
        try {
            Intent restartService = new Intent(getApplicationContext(),this.getClass());
            restartService.setPackage(getPackageName());
            PendingIntent restartServicePI = PendingIntent.getService(
                        getApplicationContext(), 1, restartService,
                        PendingIntent.FLAG_ONE_SHOT);
            AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
                alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, restartServicePI);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }