嗨我正在開發一個Android應用程序,它將從用戶手機中檢索所有通話記錄並將數據存儲在SQLite數據庫中。到此爲止我已經實現了,但現在我試圖將數據發送到遠程服務器每12小時。我創建了一個服務和一個廣播接收器,但如果檢測到互聯網連接,如何將數據發送到我的遠程服務器?如何在android中每12小時向Android中的服務器發送數據?
這是我的服務類
public class MyService extends Service {
String callNumber,deviceId;
int phoneType;
String possibleEmail;
Cursor c;
SQLiteDatabase db;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, " MyService Created ", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId) {
Toast.makeText(this, " MyService Started", Toast.LENGTH_LONG).show();
getAllCallLogs();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Toast.makeText(this, "Servics Stopped", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
private void getAllCallLogs(){
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
deviceId = tm.getDeviceId();
phoneType = tm.getPhoneType();
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
Cursor cur = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, strOrder);
// loop through cursor
while (cur.moveToNext()) {
callNumber = cur.getString(cur
.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
String callName = cur
.getString(cur
.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
String callDate = cur.getString(cur
.getColumnIndex(android.provider.CallLog.Calls.DATE));
SimpleDateFormat formatter = new SimpleDateFormat(
"dd-MMM-yyyy HH:mm:ss");
String dateString = formatter.format(new Date(Long
.parseLong(callDate)));
String callType = cur.getString(cur
.getColumnIndex(android.provider.CallLog.Calls.TYPE));
String type = null;
int type1 = Integer.parseInt(callType);
switch(type1){
case CallLog.Calls.OUTGOING_TYPE:
type = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
type = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
type = "MISSED";
break;
}
String duration = cur.getString(cur
.getColumnIndex(android.provider.CallLog.Calls.DURATION));
// process log data...
Toast.makeText(getApplicationContext(), ""+type, 5000).show();
db = this.openOrCreateDatabase("call_log", MODE_PRIVATE, null);
db.execSQL("insert into call_log(call_name,call_number,call_date,call_type,call_duration,device_id,user_name)values('"+callName+"','"+callNumber+"','"+dateString+"','"+type+"','"+duration+"','"+deviceId+"','"+possibleEmail+"')");
}
}
這是我的廣播接收器
類public class MyBroadcast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(isNetConnected(context)){
Toast.makeText(context, "Connected",5000).show();
}
else
{
Toast.makeText(context, "Not Connected", 5000).show();
}
}
public static boolean isNetConnected(Context context){
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
即使移動再次重新啓動,我的服務應該在後臺運行如何實現? – Sunil
更新了答案,檢查它。 – Kartheek