Android application without GUI - android

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.

Related

BootReceiver doesen't work

I know this has been asked tons of times, and there are hundreds of example on internet, but i want to understand what's wrong in my code.
As the title suggest i want to execute some code while the phone turn on, specifically i want to set some Alarms for getting notifications, but that's not relevant now, my problem is that the boot receiver onReceive method is never called apparently
I have the right permissions in the manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
And i've also registered the receiver in the manifest
<receiver
android:name=".BootBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
And I've already created the receiver class
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context pContext, Intent intent) {
Toast.makeText(pContext,"waiting for debugger",Toast.LENGTH_LONG).show();
android.os.Debug.waitForDebugger();
//Stuff for the alarms
}
}
Can someone explain me what i'm a failing without posting always the same examples that i see everywhere?
I want to know what's wrong in my code, not how it should be done.
PS: : I forgot to say that i need to stop the code for debugging the alarm things, but i don't think that's the problem since it doesen't even show the Toast.
UPDATE: full manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="feddycapdev.conapo.turnario" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
<activity
android:name=".Calendario"
android:label="#string/title_activity_calendario"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".Settings_Activity"
android:label="#string/title_activity_settings_" >
</activity>
<activity
android:name=".SettingGiorno"
android:label="#string/title_activity_setting_giorno" >
</activity>
<receiver
android:name=".BootBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:enabled="true" android:name=".WebNotificChecker" />
<service android:enabled="true" android:name=".Sveglia" />
<service android:enabled="true" android:name=".NotificaVigilanza" />
</application>
</manifest>
I should run the activity before the receiver?
Something has to use an explicit Intent to start one of your application's components before any manifest-registered receivers will work. For 99% of Android apps, that means that the user has to start your app from the home screen. If your app is serving as a plugin to some other app, you may not need the activity — please discuss this with the developers of the app that would be hosting your plugin.
how can I set notification if the user doesn't open activity?
You wouldn't set the notification if the user does not open the activity. Your app will only run when the user lets you run. If the user chooses not to start your app, or if the user chooses to "Force Stop" your app from within Settings, your manifest-registered receiver will not receive broadcasts.

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

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.

Toast android phone with welcome greeting message when a wireless network is available

I am building an android app which once installed helps automating some of my home controls. As the first simple feature of the app, i want the phone to toast me with welcome message whenever i enter my home.
I thought i could implement this by putting in a BroadCastReceiver with action name "android.net.wifi.WIFI_STATE_CHANGED" and i assumed, whenever a new network is available, the broadcast receiver will be notified and i could check the SSID to see if i have arrived home.
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.myfirstapp.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.myfirstapp.WifiNetworksAvailableBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
<activity
android:name="com.example.myfirstapp.WifiTester"
android:label="#string/title_activity_wifi_tester" >
</activity>
</application>
</manifest>
My WifiNetworksAvailableBroadcastReceiver looks like:
public class WifiNetworksAvailableBroadcastReceiver extends BroadcastReceiver {
protected static final String TAG = "MyApp";
#Override
public void onReceive(Context context, Intent intent) {
WifiManager mMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
Log.i(TAG, "------");
for(final ScanResult result:mMgr.getScanResults()) {
if(result.SSID.equals("<MY_HOME_WIFI_SSID>")) {
Toast.makeText(context, "Welcome home", Toast.LENGTH_LONG).show();
}
}
Log.i(TAG, "======");
}
}
Now with the app installed. I enter my home but i do not get the toast. I tried running the app in the background but still i did not get the toast. Could anyone give an direction? Is my approach correct?
Try to change the receiver with the event SCAN_RESULTS_AVAILABLE_ACTION.
Your receiver will be called every time a new wifi scan has been done, then you can check if your SSID is present in the list of available ones calling getScanResults() and eventually show the toast.
PS.
Have you registered your receiver?

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?
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)

Categories

Resources