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 のドキュメントを読み直してください。多分それは助けになるでしょう。

また、マニフェストにはアクティビティがありません。少なくとも 1 つのメイン ランチャー アクティビティを追加します。どのアプリにも少なくとも 1 つ必要です。

PS 発生したエラーに関する詳細情報を提供してください。

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

おそらく

android.intent.action.BOOT_COMPLETED
于 2013-03-20T13:04:53.760 に答える