1

我正在编写一个 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>

请让我知道这是什么问题....感谢您的时间:-)

4

2 回答 2

3
Thread.sleep(60000);

这是一个非常糟糕的主意,删除它并重新阅读 BroadcastReceiver 的文档。也许会有所帮助。

此外,您的清单中没有任何活动。添加至少一个主要的启动器活动。任何应用程序都必须至少有一个。

PS 向我们提供有关您遇到的错误的更多信息。

于 2013-03-20T13:28:23.410 回答
0
android.intent.action._BOOT_COMPLETED

应该是

android.intent.action.BOOT_COMPLETED
于 2013-03-20T13:04:53.760 回答