How to create an android app with only 1 broadcastreceiver? - android

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?

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

SHouldn't you make a service instead of an application ? (I've never created a service, but i think it should be more appropriate in your case)

Related

System App auto starting

I am making a app for a android phone development company. I need to run the app in the background there should not be any user interaction like google play service. So I made a application and placed it in the "system/app" so that user can't uninstall the app but I need to open the application to run the service in the background how can I skip that.
Look here: Android BroadcastReceiver on startup - keep running when Activity is in Background
Once you have broadcast receiver that is being called when system boots, you should start sticky service. Look here: START_STICKY and START_NOT_STICKY
That way your application will be started on system each system boot.
I think you should register for some BroadcastReceivers, so it will start yous app. You can use for example the boot_completed action so like this:
public class Autostart extends BroadcastReceiver
{
public void onReceive(Context arg0, Intent arg1)
{
Log.i("Autostart", "**********started************");
}
}
and the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pack.saltriver" android:versionCode="1" android:versionName="1.0"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>
but you can use a different type, you just need to modify the name of the intent . Here is the list of all BroadcastReceiver Intent.
There are many intent filter you can use fro ex- boot completed or Power plugged.
In both cases you can use broadcast receiver to to launch the active.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pack.saltriver" android:versionCode="1" android:versionName="1.0"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".tart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

service not started on BOOT COMPLETE

I have a service that I would like to start on BOOT COMPLETE
when it is started , I have a toast message displayed.
my problem is that when the device boots up , the toast is displayed and is stuck on screen, and the service is not starting correctly.
however if I am trying to start my service through an activity , the service is starting well and the toast disappears after a few seconds correctly.
my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tfl.extprotocolservice"
android:versionCode="7"
android:versionName="1.6" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<application
android:allowBackup="true"
android:icon="#drawable/launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="com.tfl.extprotocolservice.ExtProtocolBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".ExtProtocolService" >
<intent-filter>
<action android:name="com.tfl.extprotocolservice.ISetIpPort" />
</intent-filter>
<intent-filter>
<action android:name="com.tfl.extprotocolservice.IExtMessage" />
</intent-filter>
</service>
<!--
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
-->
</application>
</manifest>
my broadcast receiver:
public class ExtProtocolBroadcastReceiver extends BroadcastReceiver {
/* broadcast receiver to start on BOOT COMPLETE*/
#Override
public void onReceive(Context context, Intent intent) {
Intent StartServiceIntent=new Intent(context,ExtProtocolService.class);
context.startService(StartServiceIntent);
}
}
btw, the activity in the manifest is commented because I don't really need it , it was just to test starting the service from an activity.
If your application has no activities, your BroadcastReceiver will never get called.
When you install an application, it is installed in the "stopped state". applications in "stopped state" do not get broadcast Intents delivered to them.
In order to get your application out of "stopped state", the user must manually launch your application (at least once). In order to do this, you must offer him an Activity that he can use to start your application.
Once your application is no longer in "stopped state", Android will deliver broadcast Intents to it. That is, until the user "force stops" your application.
If the user "force stops" your application, it will go back to "stopped state" and will no longer get the broadcast Intents. Until the user manually starts your application again.
I tried with am broadcast -a android.intent.action.BOOT_COMPLETED then it restart the device.
You can try <action android:name="android.intent.action.USER_PRESENT"/>
After more research, I think it was the fastboot mode which will not broadcast BOOT_COMPLETE.
Your service is filtering actions, but your intent doesn't provide any.
Fix with this:
StartServiceIntent.setAction("com.tfl.extprotocolservice.IExtMessage");

BOOT_COMPLETED not called if device restarted using hardware buttons

I have registered a BroadcastReceiver called CheckReceiver:-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.captchachecker"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:icon="#drawable/ic_launcher"
android:installLocation="internalOnly"
android:label="#string/app_name"
>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".CheckReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
</manifest>
CheckReceiver code:-
public class CheckReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Check Receiver", "Captcha Receiver called");
Toast.makeText(context, "Receiver called", Toast.LENGTH_SHORT).show();
Intent service = new Intent(context, CheckService.class);
startWakefulService(context, service);
}
}
The problem is that when I press the power button and choose Power off option, BroadcastReceiver is not being called.
If I restart the device using:-
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED
then BroadcastReceiver is being called.
I have read about launching activity atleast once for BroadcastReceiver and launch an activity 2-3 times before checking.
Turns out I had used:-
adb shell pm set-install-location 2
in the past on my device. The app got installed to sd card ignoring:-
android:installLocation="internalOnly"
After I moved the app to phone it started working properly. To move the app to phone go to:-
Settings->Apps->Your app and choose "Move to Phone"
So, if your boot receiver doesn't works. Check:-
Permissions and intent filter in the manifest are declared properly.
App has atleast one activity and it has been launched atleast once by user after installing or after the app is force closed.
App has been installed to internal storage.

Android application without GUI

I have been developing a simple application without UI using broadcast receiver.
The app doesn't contain any ACTIVITIES.
I have given necessary permissions.
I took the code from this url:http://developerandro.blogspot.in/2013/09/check-internet-connection-using.html
The app shows a toast "Not connected to internet" when I click change wifi state. It's working correctly.
But my question is There is an activity registered in my manifest file which I don't have. So I delete those lines from my manifest. Then no toasts are shown and I checked the logs too. No output on changing wifi state.
Why this happened? Please help me guys...
Here is the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcast_internetcheck"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.broadcast_internetcheck.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.example.broadcast_internetcheck.NetworkChangeReceiver"
android:label="NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is my Broadcastreceiver class:
public class NetworkChangeReceiver extends BroadcastReceiver{
#Override
public void onReceive(final Context context, final Intent intent) {
String status = NetworkUtil.getConnectivityStatusString(context);
/*Above line will return the status of wifi */
Toast.makeText(context, status, Toast.LENGTH_LONG).show();
}
}
You will need to create a dummy activity for a Service which will be triggered in the onCreate() of the dummy, maybe a non-UI with finish() .
Without that the required implementation is not possible, esp above Android 3.1.
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
Run only a background service when application start
Start android application without activity
http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html
And for more on Service:
http://developer.android.com/guide/components/services.html
https://developer.android.com/training/run-background-service/create-service.html
http://www.vogella.com/tutorials/AndroidServices/article.html
You can use service instead. But showing Toast through service bit complicated instead you can show notification through service for No Internet Connection.
If you don't want any activity check this answer. Actually you would have to create service for this: link
Create a transparent activity. Launch the toast while the activity is active and immediately finish the activity.

Broadcast Receiver Not working in 4.1.1

I have a broadcast receiver for incoming call.I want to launch a new activity when an incoming call comes.I am aware of the changes that are made from android 3.0,that the broadcast receiver will not work unless user manually starts an application
For that purpose I launch a dummy activity with just a toast message in it.Still the broadcast receiver is not working.
Here is my code
My broadcastreceiver
public class IncomingCallResult extends BroadcastReceiver
{
String TAG="IncomingCallResult";
#Override
public void onReceive(Context arg0, Intent I1)
{
Log.i(TAG,"inside on receive........");
Bundle bundle=I1.getExtras();
String state=bundle.getString(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
Intent flash_intent=new Intent(arg0,LedFlasher.class);
flash_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
arg0.startActivity(flash_intent);
}
}
}
manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.blinker"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".IncomingCallResult"
android:enabled="true">
<intent-filter
android:priority="214783648"
android:name="android.intent.action.PHONE_STATE">
</intent-filter>
</receiver>
<activity
android:name=".LedFlasher"
android:label="#string/title_activity_incoming_call_result" >
</activity>
<activity
android:name=".Dummy">
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
What is wrong with the code?
Please help
As you have already known that the broadcast receiver will not work unless user manually starts an application For that purpose, you should not be surprised that your broadcast receiver will not work until user Manually open your application once. That is to say, you have to make a launcher activity which user is able to click and open it manually.
And what's more, it is better to Open and stay in your application for like 20s since I remember that the change of application configuration will take 10 or 20s to be saved.

Categories

Resources