我正在编写一个Android应用程序,以便在每分钟后在后台持续监控网络...我正在使用BroadcastReceiver ...它在启动完成时启动....并且在BroadcastReceiver内部我正在关注事
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....
但不幸的是,当我在Android设备上安装并运行它时,这个应用程序给了我一些错误
我的BroadcastReceiver代码是: -
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;
}
}
}
我的清单文件是: -
<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>
请让我知道这有什么问题....感谢您的时间: - )
答案 0 :(得分:2)
Thread.sleep(60000);
这是一个非常糟糕的主意,删除它并重新阅读BroadcastReceiver的文档。也许它会有所帮助。
此外,您的清单中没有任何活动。添加至少一个主启动器活动。任何应用必须至少有一个。
P.S。给我们更多关于你得到的错误的信息。
答案 1 :(得分:0)
android.intent.action._BOOT_COMPLETED
应该是
android.intent.action.BOOT_COMPLETED