Android应用。在模拟器中工作,但在Android设备中不起作用

时间:2013-05-27 13:00:22

标签: android broadcastreceiver android-alarms intentservice

我编写了一个简单的应用程序来获取网络详细信息,我使用AlarmManager在后台连续运行一段代码(IntentServiceImpl类)。

我从BroadcastReceiver调用AlarmManager类,我已经为BOOT_COMPLETE事件注册了BroadcastReceiver(BroadcastReceiverImpl类)...它在Android模拟器中正常工作但是它在Android设备中不起作用(我已经检查过了)在三星Galaxy s2和三星Galaxy Tab)这是源代码..

BroadcastReceiverImpl.java

public class BroadcastReceiverImpl extends BroadcastReceiver {

    private static final String LOG_TAG = BroadcastReceiverImpl.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {


        Log.d(LOG_TAG, "Inside the BroadcastReceiver Class");

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND,QOSConstants.TIME_DELAY_IN_SECONDS);

        Intent serviceIntent = new Intent(context, IntentServiceImpl.class);
        serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


        PendingIntent pendingIntent = PendingIntent.getService(context, QOSConstants.REQUEST_CODE,serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT);


        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        //After after 60 seconds
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
        QOSConstants.MILLISECONDS * QOSConstants.TIME_DELAY_IN_SECONDS, pendingIntent); 

        Log.d(LOG_TAG, "Exiting the QOSBroadcastReceiver Class");
    }
}

IntentServiceImpl.java

public class IntentServiceImpl extends IntentService implements LocationListener {

    private String strNetworkInfo;
    private LocationManager locationManager;
    private QOSDatabaseHelper qosDatabaseHelper;
    private NetworkDetailsVO networkDetailsVO ;
    private static final String LOG_TAG =  QOSIntentService.class.getName();
    private QOSServlet qosServlet;
    private QOSPhoneStateListner qosPhoneStateListner;

    public QOSIntentService() {
        super(LOG_TAG);

        Log.d(LOG_TAG , ": Inside NetworkInfoService() constructor");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        qosServlet = new QOSServlet();
        networkDetailsVO  = new NetworkDetailsVO();
        qosPhoneStateListner = new QOSPhoneStateListner();
        qosDatabaseHelper = new QOSDatabaseHelper(getApplicationContext());
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        Log.i("LOG_TAG: ", "Inside onHandleIntent(Intent) method Fetching Network Details ");

        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        final TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

        if(networkInfo!=null && networkInfo.isConnected()){

            String networkState = getNetworkStateString(networkInfo.getState());
            String stateString = networkInfo.toString().replace(',', '\n');
            strNetworkInfo = String.format(QOSConstants.NETWORK_STATE_DISPLAY_FORMAT,
            networkInfo.getTypeName(),networkState,stateString);

            networkDetailsVO.setNetworkState(networkState);
            networkDetailsVO.setNetworkType(networkInfo.getTypeName());
            networkDetailsVO.setRoaming(String.valueOf(networkInfo.isRoaming()));
            networkDetailsVO.setReason(networkInfo.getReason());
            networkDetailsVO.setFailOver(String.valueOf(networkInfo.isFailover()));
            networkDetailsVO.setNetworkAvailable(String.valueOf(networkInfo.isAvailable()));
            networkDetailsVO.setNetworkConnectivity(String.valueOf(networkInfo.isConnected()));

        if(networkInfo.getExtraInfo()!=null)
            networkDetailsVO.setExtraNetworkInfo(networkInfo.getExtraInfo());
            else
            networkDetailsVO.setExtraNetworkInfo(QOSConstants.NETWORK_EXTRA_INFO_NOT_AVAILABLE);
        }

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, QOSConstants.MINIMUM_TIME, QOSConstants.MINIMUM_DISTANCE, this);
        Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(new Criteria(), false));

        if(location!=null){

            onLocationChanged(location);
            QOSGeocoder qosGeocoder = new QOSGeocoder();
            networkDetailsVO.setLocationAddress(qosGeocoder.getLocationAddress(getApplicationContext(), location));

        }else{

            networkDetailsVO.setLatitude(0.0);
            networkDetailsVO.setLongitude(0.0);

        }

        // Identify the connectivity type. WI-FI/MOBILE.

        if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI){

            WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            networkDetailsVO.setSignalStrength(wifiManager.getConnectionInfo().getLinkSpeed());

        }else if(networkInfo.getType() == ConnectivityManager.TYPE_MOBILE){

            telephonyManager.listen(qosPhoneStateListner, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
            networkDetailsVO.setSignalStrength(qosPhoneStateListner.getStrSignalStrength());
        }

        networkDetailsVO.setNetworkInfo(strNetworkInfo);
        networkDetailsVO.setSimSerialNumber(telephonyManager.getSimSerialNumber());
        networkDetailsVO.setOperatorName(telephonyManager.getNetworkOperatorName());
        networkDetailsVO.setDateTime(new Date().toString());

        Log.i(LOG_TAG , "Network Details :- " + "\t" + networkInfo.toString());

        //qosDatabaseHelper = new QOSDatabaseHelper(getApplicationContext());
        qosDatabaseHelper.saveRecord(networkDetailsVO);
        qosServlet.invokeServlet(networkDetailsVO);
        Log.i(LOG_TAG , " Network details are saved to the Database,exiting onHandleIntent(Intent) method ");

    }

    @Override
    public void onLocationChanged(Location location) {

        Log.i(LOG_TAG, " : Inside onLocationChanged(Location) method ");

        networkDetailsVO.setLatitude(location.getLatitude());
        networkDetailsVO.setLongitude(location.getLongitude());

        Log.i(LOG_TAG, " : Exiting onLocationChanged(Location) method ");

    }

    @Override
    public void onProviderEnabled(String provider) {

        Log.i(LOG_TAG, ": Provider is Enabled !! ");

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, QOSConstants.MINIMUM_TIME, 
                QOSConstants.SECOND_MINIMUM_DISTANCE, this);


    }

    @Override
    public void onProviderDisabled(String string) {

        Log.i(LOG_TAG, ": Provider is Disabled !! ");
    }

    @Override
    public void onStatusChanged(String strStatus, int arg1, Bundle bundle) {

        Log.i(LOG_TAG, " : Inside onStatusChanged(String,int,Bundle) method ");
    }

    private String getNetworkStateString(NetworkInfo.State state){

        Log.i(LOG_TAG, ": Inside getNetworkStateString(NetworkInfo.State)");

        String stateString = QOSConstants.NETWORK_STATE_UNKNOWN;

        switch(state)
        {
        case CONNECTED: 
            stateString = QOSConstants.NETWORK_STATE_CONNECTED;
            break;

        case CONNECTING:        
            stateString = QOSConstants.NETWORK_STATE_CONNECTING;
            break;

        case DISCONNECTED:      
            stateString = QOSConstants.NETWORK_STATE_DISCONNECTED;  
            break;

        case DISCONNECTING:     
            stateString = QOSConstants.NETWORK_STATE_DISCONNECTING;  
            break;

        case SUSPENDED:
            stateString = QOSConstants.NETWORK_STATE_SUSPENDED;
            break;

        default: 
            stateString = QOSConstants.NETWORK_STATE_UNKNOWN;

            break;

        }

        return stateString;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(qosDatabaseHelper!=null){
            qosDatabaseHelper.closeDatabase();
        }
    }
}



        @Override
        public void onProviderDisabled(String string) {

            Log.i(LOG_TAG, ": Provider is Disabled !! ");
        }


        /*@Override
        public IBinder onBind(Intent intent) {
            Log.i("LOG_TAG: ", "Inside onBind(Intent) method");
            return null;
        }
        */


        @Override
        public void onStatusChanged(String strStatus, int arg1, Bundle bundle) {

            Log.i(LOG_TAG, " : Inside onStatusChanged(String,int,Bundle) method ");
        }


        private class SignalStrengthDetector extends PhoneStateListener{


            @Override
            public void onSignalStrengthsChanged(SignalStrength signalStrength) {
                super.onSignalStrengthsChanged(signalStrength);
                networkDetailsVO.setSignalStrength(signalStrength.getGsmSignalStrength());
                Log.i(LOG_TAG, "Signal Strength :-" + String.valueOf(signalStrength.getGsmSignalStrength()));

            }

        }


        private String getNetworkStateString(NetworkInfo.State state){

            Log.i(LOG_TAG, ": Inside getNetworkStateString(NetworkInfo.State)");

            String stateString = NetworkInfoConstants.NETWORK_STATE_UNKNOWN;

            switch(state)
            {
            case CONNECTED: 
                stateString = NetworkInfoConstants.NETWORK_STATE_CONNECTED;             
                break;

            case CONNECTING:        
                stateString = NetworkInfoConstants.NETWORK_STATE_CONNECTING;    
                break;

            case DISCONNECTED:      
                stateString = NetworkInfoConstants.NETWORK_STATE_DISCONNECTED;  
                break;

            case DISCONNECTING:     
                stateString = NetworkInfoConstants.NETWORK_STATE_DISCONNECTING;  
                break;

            case SUSPENDED:         
                stateString = NetworkInfoConstants.NETWORK_STATE_SUSPENDED;              
                break;

            default: 
                stateString = NetworkInfoConstants.NETWORK_STATE_UNKNOWN;     

                break;

            }

            return stateString;
        }

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pervazive.qualityofservice_v01"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <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_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.pervazive.qualityofservice_v01.activity.QOSActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@style/FullscreenTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name="com.pervazive.qualityofservice_v01.intentservice.<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pervazive.qualityofservice_v01"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <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_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.pervazive.qualityofservice_v01.activity.QOSActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@style/FullscreenTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name="com.pervazive.qualityofservice_v01.intentservice.IntentServiceImpl"
            android:enabled="true" >
        </service>

        <receiver android:name="com.pervazive.qualityofservice_v01.broadcastreceiver.BroadcastReceiverImpl" >
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>

        </receiver>
    </application>

</manifest>

请告诉我们错误是什么...请告诉我在哪里可以找到Android设备中的日志文件...(因为它在Android模拟器上运行但在Android设备上没有运行i = 。)提前致谢

1 个答案:

答案 0 :(得分:-1)

右键点击您的项目&gt; Android工具&gt;导出为已签名的Android应用程序 填写必填字段,你就可以了。