I want to check the internet connection constantly and close the app with a warning message if connection is lost. How can i manage to do that?
Check Internet Connectivity via Phone Background service (such as AlermManager Service) then close the app if no connection found.
thanks.
close the app
Don't try to kill the process and its not recommended way of closing application. Either call finish() on all activities or call moveTaskToBack(true).
For Solution
Here you go.
You will need to register for and handle BroadCastReceiver android.net.conn.CONNECTIVITY_CHANGE
Step 1
Include following permission in manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Step2
Let Android know which class will be register for BroadCast Receiver.
<receiver android:name="ConnectivityReceiver_package_name.ConnectivityReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
Step 3
Put your logic for various Network States.
public class ConnectivityReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,false);
if(noConnectivity){
//Show Warning Message
//Close Application the way i suggested
}
}
}
Related
I am trying to show a toast message when receiving an incoming call/outgoing call.
The receiver is not working if the app is closed.
I do not want to use Service. Please help me out.
'I am using the below receiver code'
public class CallReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
if (isConnected(context)) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Toast.makeText(context, "Call in progress", Toast.LENGTH_LONG).show();
}
}
}
'This is receiver registered in manifest'
<receiver android:name="com.example.android.testapplication.CallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
<action android:name="android.intent.action.new_outgoing_call"></action>
</intent-filter>
</receiver>
Try adding the phone state permission to your manifest.
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
By default when you register a BroadCastReceiver with AndroidOS that means Receiver always work as the Service part even your application is not working, since you do not have to worry about this problem.
I think the problem is the way you register you Receiver was not correct.
With in/out coming call you should use PhoneStateListener which has overrided method onCallStateChanged. You can use 3 states over there.
Maybe this example will be helpful.
I want to build a android background service for check data from MySQL data base.Normally I doing extend from Service class and when start the app,I run service using startService() method.But problem is if i remove the app from task manager,the service is also stopped.Another thing is I want to start this service when start the device,I mean beginning.How I implement this.Help me.
When you kill your app, the service is going to restart, not be removed. You can decrale the flag to define the break point when service restart, and write some 'if' 'else' to do thing after this break point.
And if you want to start serivce when start the device, just create the broadcastReceive call 'autoStart'.
In Manifest:
<receiver android:name=".autoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and in autoStart class:
public class autoStart extends BroadcastReceiver
{
public void onReceive(Context ctx, Intent arg1)
{
Intent intent = new Intent(ctx,yourservice.class);
ctx.startService(intent);
Log.i("Autostart", "started");
}
}
When started device, system will detect on boot completed, and call this autoStart BroadcastReceive, and call your service from here
I am working on an application which will start an activity on receiving a ACTION_SCREEN_ON broadcast message.
How to implement this? Can anyone post the code for it?
I know that I have to use BroadcastReceiver. But I need a detailed explanation as I am a beginner.
Create in your application's AndroidManifest.xml a receiver with a intent-filter to catch android.intent.action.SCREEN_ON:
<receiver
android:name="your.app.packagename.Receiver" >
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
Then, create the receiver class:
package your.app.packagename;
// imports here
public class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null && Intent.ACTION_SCREEN_ON.equals(action)) {
// Do whatever you need to be done when screen turns on
}
}
}
Realize that:
You need launch your app at least one time before it starts receiving the ACTION_SCREEN_ON broadcast.
The onReceive() method is expected to run fast. If you are going to do heavy work, launch an AsyncTask or a background thread for.
I am working with Android.
I have an app I am working on uses an Activity to setup specific user input values that are then used by a service to provide alerts based on those values. Doing the research I determined how I could get the app to start up when the phone boots, however, what I really want is to have the service start but not have the app load to the screen. Currently the entire app loads to the screen when I turn on the device and then I have to exit out of it.
I have downloaded similar programs that have interfaces for settings but otherwise run in the background. How is that done?
First you have to create a receiver:
public class BootCompletedReceiver extends BroadcastReceiver {
final static String TAG = "BootCompletedReceiver";
#Override
public void onReceive(Context context, Intent arg1) {
Log.w(TAG, "starting service...");
context.startService(new Intent(context, YourService.class));
}
}
Then add permission to your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and register intent receiver:
<receiver android:name=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
After this is done, your application (Application class) will run along with services, but no Activities.
Ah, and don't put your application on SD card (APP2SD or something like that), because it has to reside in the main memory to be available right after the boot is completed.
Is there a way that trough the application, I can subscribe and unsubscribe for the "ACTION_BOOT_COMPLETED" message?
If so, how can I do this?
Any kind of pointer will help me.
Thanks in advance,
Regards,
Vinay
Check out my answer to this question. And these links.
http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/
Trying to start a service on boot on Android
Android: how to start a service at boot based on user-settings?
The best way to do this is use PackageManager.setComponentEnabledSetting(): http://developer.android.com/reference/android/content/pm/PackageManager.html#setComponentEnabledSetting(android.content.ComponentName, int, int)
So simply decide whether you want your receiving to be enabled or not by default by setting android:enabled in the manifest. Then use this API to explicitly enable or disable the component at run time as desired. If the component is disabled, at boot it will not be available, and thus not receive the broadcast.
As a matter of fact, there is:
Your boot receiver:
public class BootstrapReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context cntxt, Intent intent) {
String action = intent.getAction();
if(Intent.ACTION_BOOT_COMPLETED.equals(action)) {
// do your thing
}
}
}
Add a receiver and a permission for receiving boot events:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
....
<receiver android:name=".BootstrapReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Via the application (Use this if you want to programatically add/remove boot receiver and not via AndroidManifest.xml. But remember, you will still have to declare the boot permission in your AndroidManifest.xml)
Context ctx = getContext();
BootstrapReceiver booter = new BootstrapReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
ctx.registerReceiver(booter, filter);
Unregister:
ctx.unregisterReceiver(booter);