我正在开发一个监控网络详细信息的应用程序.....为此我使用的是BrocastReceiver和服务组件...我的BroadcastReceiver在设备启动时实例化并调用Service类...
在Service类中,我获取NetworkInfo,Signalstrength,Latitude,经度和准确度,并将这些详细信息存储到数据库中......
我的问题是: - 我可以创建数据库但是数据库没有保存在数据库中......
我的BroadcastReceiver类是: -
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.getTimeInMillis(), 60000, service);
}
}
我的服务类: -
public class NetworkInfoService extends Service implements LocationListener{
private String strNetworkInfo;
private LocationManager locationManager;
private double latitude;
private double longitude;
private double accuracy;
private int intSignalStrength;
private NetworkDetailsVO networkDetailsVO = new NetworkDetailsVO();
private NetworkInfoDatabaseHelper networkInfoDatabaseHelper;
@Override
public void onCreate() {
super.onCreate();
}
@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, 10000, 10f, 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.setSignalStrength(String.valueOf(intSignalStrength));
networkDetailsVO.setLongitude(longitude);
networkDetailsVO.setLatitude(latitude);
networkDetailsVO.setAccuracy(accuracy);
networkInfoDatabaseHelper = new NetworkInfoDatabaseHelper(getApplicationContext());
networkInfoDatabaseHelper.saveRecord(networkDetailsVO);
}
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) {
latitude = location.getLatitude();
longitude = location.getLongitude();
accuracy = 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, 10000, 10f, 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);
intSignalStrength = signalStrength.getGsmSignalStrength();
}
}
}
我的清单文件: -
<?xml version="1.0" encoding="utf-8"?>
<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.pervazive.networkinfodemo_v_03.activity.service.NetworkInfoService" >
</service>
<receiver android:name="com.example.networkinfodemo_v_03.activity.broadcastreceiver.NetworkInfoListner" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<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>
助手类代码: -
public class NetworkInfoDatabaseHelper {
private NetworkInfoDatabaseOpenHelper networkInfoDatabaseOpenHelper;
private SQLiteDatabase sqLiteDatabase;
public static final String DATABASE_NAME = "networkInfo.db";
public static final String TABLE_NAME = "networkinfo";
public static final int DATABASE_VERSION = 2;
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NETWORK_INFO = "networkInfo";
public static final String COLUMN_PHONE_NUMBER = "phonenumber";
public static final String COLUMN_OPERATOR_NAME = "operatorname";
public static final String COLUMN_SIGNAL_STRENGTH = "signalstrength";
public static final String COLUMN_LATITUDE = "latitude";
public static final String COLUMN_LONGITUDE = "longitude";
public static final String COLUMN_ACCURACY = "accuracy";
public static final String COLUMN_TIME = "time";
public NetworkInfoDatabaseHelper(Context context) {
networkInfoDatabaseOpenHelper = new NetworkInfoDatabaseOpenHelper(context);
sqLiteDatabase = networkInfoDatabaseOpenHelper.getWritableDatabase();
}
private class NetworkInfoDatabaseOpenHelper extends SQLiteOpenHelper{
public NetworkInfoDatabaseOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_NAME + "( "
+ COLUMN_ID + " INTEGER PRIMARY KEY, "
+ COLUMN_NETWORK_INFO + " TEXT, "
+ COLUMN_PHONE_NUMBER + " TEXT, "
+ COLUMN_OPERATOR_NAME + " TEXT, "
+ COLUMN_SIGNAL_STRENGTH + " TEXT, "
+ COLUMN_LATITUDE + " REAL, "
+ COLUMN_LONGITUDE + " REAL, "
+ COLUMN_ACCURACY + " REAL, "
+ COLUMN_TIME + " TEXT ) ");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
}
public void saveRecord(NetworkDetailsVO networkDetailsVO){
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_NETWORK_INFO, networkDetailsVO.getNetworkInfo());
contentValues.put(COLUMN_PHONE_NUMBER, networkDetailsVO.getPhoneNumber());
contentValues.put(COLUMN_OPERATOR_NAME, networkDetailsVO.getOperatorName());
contentValues.put(COLUMN_SIGNAL_STRENGTH, networkDetailsVO.getSignalStrength());
contentValues.put(COLUMN_LATITUDE, networkDetailsVO.getLongitude());
contentValues.put(COLUMN_LONGITUDE, networkDetailsVO.getLatitude());
contentValues.put(COLUMN_ACCURACY, networkDetailsVO.getDateTime());
contentValues.put(COLUMN_TIME, networkDetailsVO.getDateTime());
sqLiteDatabase.insert(TABLE_NAME, null, contentValues);
}
public Cursor getRecord(){
return sqLiteDatabase.rawQuery(" SELECT * FROM " + TABLE_NAME, null);
}
请让我知道错误是什么
答案 0 :(得分:1)
我做了类似的事情:
public class VoiceLaunchReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context ctx, Intent intent) {
Intent service = new Intent(ctx, VoiceLaunchService.class);
ctx.startService(service);
}
}