Create an automatic launch-able Broadcast Receiver in Android

丶灬走出姿态 提交于 2019-12-20 07:29:48

问题


Problem:

I am willing to create an application that simply starts as a background process and whenever a new message comes into the device it should log it into a file or simply display a toast message.

I have read a lot of blogs and tried to follow the steps as mentioned. But, I keep on sending messages on my device and nothing displayed not even in device log. I want to run it on devices from Froyo to Lollipop. So, I am not willing to use new Telephony API which supports API 19 and later versions.

Manifest File

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.abc.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >



        <receiver android:name=".SMSHandler">

            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

Source File

package com.abc.test;

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

public class SMSHandler extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Toast toast = Toast.makeText(context, "message initiated",
                Toast.LENGTH_LONG);
        toast.show();

        if (intent.getAction()
                .equals("android.provider.Telephony.SMS_RECEIVED")) {
            toast = Toast.makeText(context, "message received",
                    Toast.LENGTH_LONG);
            toast.show();
        }
    }

}

Environment:

IDE:

  • Android Studio

Min SDK Version:

  • 8

Tested On:

  • ICS Device (Sony Xperia U)
  • Kit-Kat (MOTO G)

回答1:


You need to add an activity, then run that activity, before this BroadcastReceiver will work.

More accurately, something needs to use an explicit Intent before your app will be moved out of the stopped state and allow manifest-registered BroadcastReceivers to work. The simplest way to do that is to have a launcher activity, and run that activity from the launcher.

To learn more, see "Launch controls on stopped applications" in the Android 3.1 release notes, along with this blog post.




回答2:


Your Code look like this in manifest file

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

Add the following to your<receiver> in the manifest:

android:enabled="true" android:exported="true">

Furthermore, according to this thread, it seems that you have to manually start one of your activities before the broadcast receiver will start working, i.e. the application has to have been launched at least once before any broadcast receiver will work.



来源:https://stackoverflow.com/questions/26578465/create-an-automatic-launch-able-broadcast-receiver-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!