Issue
i am writing an android application to continuously monitor the network in the background after every minute... i am using a BroadcastReceiver... it starts when the boot is completed.... and inside the BroadcastReceiver i am doing following things
a. Monitor Network
b. check signal strength
c. fetch longitude and latitude
d. fetch phonenumber
e. fetch operatorname
and i am saving all the above information into a Database table....
but unfortunately this application is giving me some errors when i install and run it on android device
my BroadcastReceiver code is :-
public class NetworkInfoReceiver extends BroadcastReceiver{
private NetworkInfoVO networkInfoVO = new NetworkInfoVO();
private String strNetworkInfo = QualityofServiceConstants.DEFAULT_NETWORK_INFO;
private SignalStrengthPhoneStateListner signalStrengthPhoneStateListner = new SignalStrengthPhoneStateListner();
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Started", Toast.LENGTH_SHORT).show();
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
//Check Network status here
if(networkInfo!=null && networkInfo.isConnected()){
String networkState = getNetworkStateString(networkInfo.getState());
String stateString = networkInfo.toString().replace(',', '\n');
strNetworkInfo = String.format("Network Type: %s\n Network State: %s\n\n %s",
networkInfo.getTypeName(),networkState,stateString);
}
//check signal strength here
telephonyManager.listen(signalStrengthPhoneStateListner, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
networkInfoVO.setNetworkInfo(strNetworkInfo);
//get phonenumber here
networkInfoVO.setPhoneNumber(telephonyManager.getLine1Number());
//get operator name here
networkInfoVO.setServiceProvider(telephonyManager.getNetworkOperatorName());
//get signal strength here networkInfoVO.setSignalStrength(signalStrengthPhoneStateListner.getSignalStrengths());
//get latitude here
networkInfoVO.setLatitude(String.valueOf(getGPS(context).getLatitude()));
//get longitude here
networkInfoVO.setLongitude(String.valueOf(getGPS(context).getLongitude()));
networkInfoVO.setTime(new Date().toString());
new NetworkInfoDatabaseHelper(context).saveRecord(networkInfoVO);
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String getNetworkStateString(NetworkInfo.State state){
String stateString = QualityofServiceConstants.NETWORK_STATE_UNKNOWN;
switch(state)
{
case CONNECTED:
stateString = QualityofServiceConstants.NETWORK_STATE_CONNECTED;
break;
case CONNECTING:
stateString = QualityofServiceConstants.NETWORK_STATE_CONNECTING;
break;
case DISCONNECTED:
stateString = QualityofServiceConstants.NETWORK_STATE_DISCONNECTED;
break;
case DISCONNECTING:
stateString = QualityofServiceConstants.NETWORK_STATE_DISCONNECTING;
break;
case SUSPENDED:
stateString = QualityofServiceConstants.NETWORK_STATE_SUSPENDED;
break;
default:
stateString = QualityofServiceConstants.NETWORK_STATE_UNKNOWN;
break;
}
return stateString;
}
private Location getGPS(Context context){
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
List<String>providers = locationManager.getProviders(true);
Location location = null;
for (int i = providers.size()-1; i >= 0; i--){
location = locationManager.getLastKnownLocation(providers.get(i));
if(location!=null)
break;
}
return location;
}
private class SignalStrengthPhoneStateListner extends PhoneStateListener{
private String signalStrengths;
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
if(signalStrength.getGsmSignalStrength() > 30){
signalStrengths = "Signal Strength :" + signalStrength.getGsmSignalStrength() + " Good";
}else if(signalStrength.getGsmSignalStrength() > 20 && signalStrength.getGsmSignalStrength() < 30){
signalStrengths = "Signal Strength : " + signalStrength.getGsmSignalStrength() + " Average";
}else if(signalStrength.getGsmSignalStrength() < 20){
signalStrengths = "Signal Strength : " + signalStrength.getGsmSignalStrength() + " Weak";
}
}
public String getSignalStrengths() {
return signalStrengths;
}
}
}
my manifest file is :-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="compervazive.lognetworkinfo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_CORSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="com.example.qos.broadcastreceiver.NetworkInfoReceiver" >
<intent-filter>
<action android:name="android.intent.action._BOOT_COMPLETED"/>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
<action android:name="android.intent.action.SIG_STR"/>
</intent-filter>
</receiver>
</application>
</manifest>
please let me know what is the problem in this.... Thanks for your time :-)
Solution
Thread.sleep(60000);
This is very bad idea, remove this and reread BroadcastReceiver's documentation. Maybe it will help.
Also you do not have any activity in your manifest. Add at least one main launcher activity. Any app must have at least one.
P.S. give us more info about errors you get.
Answered By - Leonidos
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.