如何在没有 GUI 的情况下使用带有 BroadcastReceiver 的 Android 服务接收 SMS

How to receive SMS using Android service with BroadcastReceiver without GUI

我一直在开发一个应用程序,该应用程序用于接收 SMS 并使用 Broadcast Receiver 显示 Toast 并且我有一个 activity 没有任何目的,当我删除activity 并在我的 phone 上构建 apk 和 运行,应用程序在收到 SMS 时没有响应(没有 Toast 显示),尽管剩余的代码与以前相同。任何人都可以帮助我,我被困住了,无法帮助自己阅读数百个答案。我研究过我应该创建一个服务,但仍然没有出现 Toast。下面是我的代码。我的应用程序中不能有任何 GUI,甚至不想自动终止 activity.

BroadcastReceiver.java

package com.test.testservice;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;



public class SmsReceiver extends BroadcastReceiver {

    public static final String SMS_BUNDLE = "pdus";
    private static final String LOG = "SmsBroadcastReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle intentExtras = intent.getExtras();
        if (intentExtras != null) {
            Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);


            if (sms != null)
            {
                String smsMessageStr = "";

                for (int i = 0; i < sms.length; ++i)
                {
                    SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);

                    String smsBody = smsMessage.getMessageBody().toString();
                    String address = smsMessage.getOriginatingAddress();

                    smsMessageStr += "SMS From: " + address + "\n";
                    smsMessageStr += smsBody + "\n";
                }
                Toast.makeText(context, smsMessageStr, Toast.LENGTH_LONG).show();
                //MyService objService=new MyService();
                //objService.startService(intent);
                //objService.stopService(intent);


                Intent myIntent = new Intent(context, MyService.class);
                //myIntent.putExtra("Sender", Sender);
                //myIntent.putExtra("Fullsms", Fullsms);
                context.startService(myIntent);


            }
        }
    }
}

MyService.java

package com.test.testservice;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
    private static final String LOG = "MyService";
    @Override
    public boolean stopService(Intent name) {
        if (super.stopService(name))
        {
            Toast.makeText(this,"HELLO stopService",Toast.LENGTH_LONG).show();
            Log.i(LOG, "stopService");
            return true;
        }
        else return false;
    }

    @Override
    public ComponentName startService(Intent service) {
        return super.startService(service);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this,"HELLO onCreate",Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Toast.makeText(this,"HELLO onStart",Toast.LENGTH_LONG).show();
    }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.test.testservice">

    <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">


        <receiver android:name=".SmsReceiver" android:enabled="true" android:exported="true">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.REBOOT"/>
            </intent-filter>
        </receiver>

    </application>

    <service android:name="com.test.testservice.service.MyService"/>

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
</manifest>

除非您正在创建自己的 phone,或者您自己的自定义 ROM,否则您需要 activity。当您的应用程序首次安装时,您的 BroadcastReceiver 将无法使用。它只会在用户启动您的 activity(或其他使用显式 Intent 来启动您的组件之一)后开始工作。

I can not have any GUI in my application

那么您的应用将无法在 Android 3.1+ 设备上运行,这些设备构成了 Android 设备生态系统的 广大 大部分。