Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an android app with only 1 broadcastreceiver?

I am trying to create an application in Android that is composed of only 1 broadcastreceiver (and nothing else).

The broadcastreceiver should simply catch the broadcast(for example sms message received,log the info and finish). However, I noticed that broadcast is not caught by the receiver, unless I indicate I have main Activity as the following AndroidManifest.xml will show:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name="com.myapp.MyBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

    <activity
        android:name="com.myapp.MainActivity"
        android:label="@string/activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

I don't even have to have an Activity class within the application. Also, if I remove either android.intent.category.LAUNCHER or android.intent.action.MAIN in the intent filter, it does not worrk either. The behavoir is the same on my phone and the emulator which are both running android 4.2

my Broadcastreceiver class looks like this:

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,intent.getAction(),Toast.LENGTH_SHORT).show();
    }
}

Is it not possible to have an app with only a broadcastreceiver?

like image 646
Ido Avatar asked Mar 27 '13 10:03

Ido


1 Answers

Starting from Android 3.1 (API 12), app cannot receive broadcasts until a UI component of an app (an Activity) has been manually opened by the user at least once. Even if user force stop the application , same is applied.

Reference : http://developer.android.com/about/versions/android-3.1.html#launchcontrols

like image 177
android.fryo Avatar answered Oct 07 '22 07:10

android.fryo