我开发了一个应用程序来监控每分钟后的网络状态....我正在使用服务组件来监控网络状态并将网络详细信息存储到数据库中。我正在使用AlarmManager从广播接收器调用服务类class ....我正在重复AlarmManager按顺序在每分钟后调用服务类....但遗憾的是服务类没有在每分钟后被调用...相反它被调用了一段时间它会停止......我在这里附上了源代码....
请告诉我如何在每分钟后使用AlarmMagerclass在后台连续运行Service类...
提前致谢..; - )
我的服务类
public class NetworkInfoService extends Service implements LocationListener{
private String strNetworkInfo;
private LocationManager locationManager;
private NetworkDetailsVO networkDetailsVO = new NetworkDetailsVO();
private NetworkInfoDatabaseHelper networkInfoDatabaseHelper;
@Override
public void onCreate() {
super.onCreate();
ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
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);
}
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
/*locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 300f, this);*/
telephonyManager.listen(new SignalStrengthDetector(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
networkDetailsVO.setNetworkInfo(strNetworkInfo);
networkDetailsVO.setPhoneNumber(telephonyManager.getLine1Number());
networkDetailsVO.setOperatorName(telephonyManager.getNetworkOperatorName());
networkDetailsVO.setDateTime(new Date().toString());
Log.i("Network DetailsFrom Service", networkInfo.toString());
networkInfoDatabaseHelper = new NetworkInfoDatabaseHelper(getApplicationContext());
networkInfoDatabaseHelper.saveRecord(networkDetailsVO);
}
@Override
public void onStart(Intent intent, int startId) {
ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
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);
}
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
/*locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 300f, this);*/
telephonyManager.listen(new SignalStrengthDetector(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
networkDetailsVO.setNetworkInfo(strNetworkInfo);
networkDetailsVO.setPhoneNumber(telephonyManager.getLine1Number());
networkDetailsVO.setOperatorName(telephonyManager.getNetworkOperatorName());
networkDetailsVO.setDateTime(new Date().toString());
Log.i("Network DetailsFrom Service", networkInfo.toString());
networkInfoDatabaseHelper = new NetworkInfoDatabaseHelper(getApplicationContext());
networkInfoDatabaseHelper.saveRecord(networkDetailsVO);
}
/*
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}*/
private String getNetworkStateString(NetworkInfo.State state){
String stateString = "unknown";
switch(state)
{
case CONNECTED:
stateString = "connected";
break;
case CONNECTING:
stateString = "connecting";
break;
case DISCONNECTED:
stateString = "disconnected";
break;
case DISCONNECTING:
stateString = "disconnecting";
break;
case SUSPENDED:
stateString = "suspended";
break;
default:
stateString = "unknown";
break;
}
return stateString;
}
@Override
public void onLocationChanged(Location location) {
networkDetailsVO.setLatitude(location.getLatitude());
networkDetailsVO.setLongitude(location.getLongitude());
networkDetailsVO.setAccuracy(location.getAccuracy());
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onProviderDisabled(String string) {
}
@Override
public void onProviderEnabled(String provider) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, this);
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
private class SignalStrengthDetector extends PhoneStateListener{
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
networkDetailsVO.setSignalStrength(signalStrength.getGsmSignalStrength());
}
}
}
My Broadcast Receiver类(注册android.intent.action.BOOT_COMPLETED意图)
public class NetworkInfoListner extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
PendingIntent service = null;
Intent intentForService = new Intent(context.getApplicationContext(), NetworkInfoService.class);
final AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
final Calendar time = Calendar.getInstance();
time.set(Calendar.MINUTE, 0);
time.set(Calendar.SECOND, 0);
time.set(Calendar.MILLISECOND, 0);
if (service == null) {
service = PendingIntent.getService(context, 0,
intentForService, PendingIntent.FLAG_CANCEL_CURRENT);
}
alarmManager.setRepeating(AlarmManager.RTC, time.getTime()
.getTime(), 60000, service);
}
我的清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.networkinfodemo_v_03"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<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" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.networkinfodemo_v_03.activity.NetworkInfoActivity"
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.example.networkinfodemo_v_03.service.NetworkInfoService" android:enabled="true">
</service>
<receiver android:name="com.example.networkinfodemo_v_03.broadcastreceiver.NetworkInfoListner" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
请让我知道错误是什么......
答案 0 :(得分:0)
试试这个
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 60);
service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), 60000, pending);