I want to check whether internet is present or no continuously in background service so that I get broadcast as soon as the device gets connected to internet. Suggest with some snippets. Thank you.
Create class that extends BroadcastReceiver , use ConnectivityManager.EXTRA_NO_CONNECTIVITY to get connection info.
Code :
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.widget.Toast;
public class CheckConnectivity extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent arg1) {
boolean isConnected = arg1.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if(isConnected){
Toast.makeText(context, "Internet Connection Lost", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(context, "Internet Connected", Toast.LENGTH_LONG).show();
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.connect.broadcast"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:exported="false"
android:name=".CheckConnectivity" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>
Rather than creating Service just create intent-filter with connectivity change Developer Android
<receiver android:enabled="true" android:name=".YourReceiver"
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
With your BroadcastReciever:
public class YourReceiver extends BroadcastReceiver
{
#Override
public void onReceive( Context context, Intent intent )
{
}
}
Related
I'm wondering why this simple receiver for detecting connection change don't work for me, it is like with al other sample which i search on google
in this sample application i have only one activity and broadcast
BroadcastReceiver:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class ConnectivityReceiver extends BroadcastReceiver {
public ConnectivityReceiver() {
super();
}
#Override
public void onReceive(Context context, Intent arg1) {
Log.e("onReceive ", " , ");
}
}
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.sampleapp">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver
android:name=".ConnectivityReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>
</manifest>
after turning on/off i dont have any output on logcat
and its my activity:
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
If your application is targeting Android N, then you must have to register your CONNECTIVITY_CHANGE broadcast dynamically Context.registerReceiver().
Reference:
Android N Background Optimizations
I have a problem with running the service. In the logs there is nothing to see that the service is running. So I don't know that she works. Toast also does not show in MainActivity. I read a lot of posts and none work. How to fix it?
Service
import android.app.Service;
import android.os.IBinder;
import android.widget.Toast;
import android.content.Intent;
public class AutoStartUp extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
// do something when the service is created
}
}
BroadcastReceiver
import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.Intent;
public class BootComplete extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, AutoStartUp.class);
context.startService(serviceIntent);
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kamiszczu.ovh.servicetest3">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" >
</uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver
android:name="kamiszczu.ovh.servicetest3.BootComplete"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="kamiszczu.ovh.servicetest3.AutoStartUp"
android:enabled="true">
</service>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You don't have to verify the intent type since the
Receiver is registered with only one intent type(Intent.ACTION_BOOT_COMPLETED). So there is no need to check in the receiver if the intent action is Intent.ACTION_BOOT_COMPLETED.
I think the condition in the receiver is not true and because of that the code that starts your service is not executed.
I've tried:
This which doesn't work on my phone:
Trying to start a service on boot on Android
This which also fails to work properly:
http://www.compiletimeerror.com/2014/12/android-autostart-app-after-boot-with.html#.VpL6sxWLTIU
And a few others which I couldn't find again. Could someone please post a working example of code which will start a MainActivity with the default HelloWorld xml layout?
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".MyBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity
android:name=".MyService"
android:label="#string/title_activity_my_service" >
</activity>
</application>
</manifest>
package com.example;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MyService extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_service);
}
}
First:create a BroadcastReceiver, onReceive(Context context, Intent intent),start the app you want to startup in this method
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
xxx.class is the app you want to start
Intent service = new Intent(context,XXXclass);
context.startService(service);
Log.v("TAG", "you want to startup this app.....");
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
context.startActivity(intent );
} }
Secondly:configure receiver and intent-filter
<receiver android:name="BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
THirdly,add permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
im trying to start an application on device boot.
my code is as follow
1- firstly the main class which contain a background thread (asynctask) that send data to a mysql database.
2- Service class
package com.seven.ex.helper;
import com.seven.ex.AndroidGPSTrackingActivity;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class service extends Service {
private static final String TAG = "MyService";
#Override
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
#Override
public void onStart(Intent intent, int startid)
{
Intent intents = new Intent(getBaseContext(),AndroidGPSTrackingActivity.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
}
3- BootUpReceiver class
package com.seven.ex.helper;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context arg0, Intent arg1) {
Intent intent = new Intent(arg0,service.class);
arg0.startService(intent);
Log.i("Autostart", "started");
}
}
4- the android manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gpstracking"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.seven.gpstracking.AndroidGPSTrackingActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".service"
android:label="#string/app_name"
>
</service>
<receiver android:enabled="true" android:name=".BootUpReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
The main method is working correctly before i tried to start it on boot.
Also the app still working after i ignore the android error on boot (The App Closed)
in the log cat am getting a class not found exception
can you please help ?
Your Service won't work like it is at the moment. You will have to move the stuff from onStart() to onStartCommand() and then return whether the service is sticky or not. The problem is that the onStart()-method has a good chance of not getting called at all (as it is deprectaed by now).
#Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
//do your stuff here
return Service.START_NOT_STICKY;
}
In your manifest you have assigned package: com.example.gpstracking. When u then define .BootUpReceiver. The system should expect to locate the class at com.example.gpstracking.BootUpReceiver.
Please try and change the package .BootUpReceiver to the full path com.seven.ex.helper.BootUpReceiver. The same goes for AndroidGPSTrackingActivity as this should be com.seven.ex.AndroidGPSTrackingActivity as far as I can tell.
I am currently working on an app that uses a broadcastreceiver to check for incoming text messages, but all of the sudden it seems to have stopped working, I even wrote a small test application that, to me at least, seems syntactically correct, but also is not functioning.
Here is the code from the test project:
The manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AGApplications.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".test"
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=".MsgMon">
<intent-filter>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
<uses-sdk android:minSdkVersion="8" />
</manifest>
The BroadcastReceiver:
package com.AGApplications.test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MsgMon extends BroadcastReceiver{
#Override
//Called when new message is received
public void onReceive(Context context, Intent intent) {
Log.d("PHONE", "Message Received");
}
}
And the main activity:
package com.AGApplications.test;
import android.app.Activity;
import android.os.Bundle;
public class test extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
As far as I can tell I'm not doing anything wrong, but obviously I am!
So I discovered the issue. You cannot use "PHONE" as a debug tag. Don't ask me why, as I cannot seem to figure it out, but after using multiple different tags, "PHONE" was the only one that wouldn't register.
My bug seems to lie elsewhere and I am hot on the trail again! Thank you all for your help!
All seems to ok for me i have done same example
here is my code :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.infostretch.broadcastex"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name="broadcastex" android:label="#string/app_name"><intent-filter><action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
</manifest>
public class broadcastex extends BroadcastReceiver {
/** Called when the activity is first created. */
private static final String TAG = "smsfwd";
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Intent recieved: " + intent.getAction());
Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());
if (messages.length > -1) {
Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());
}
}
this code is working for me in android 2.1 have a try i think all is same as you