2017-03-01 28 views
0

我正在開發一個應用程序,如果我的電話丟失了,我需要從我的電話讀取其他電話的未讀短信。我試圖更新數據庫中未讀的短信半分鐘。但只有當我打開應用程序時才更新。但我需要自動更新它。任何人都可以幫助我改善這一點。或者建議採取其他方式來做這件事。我已經在下面發佈了我的代碼。如何從另一個電話讀取一個電話的未讀短信

import android.database.Cursor; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Handler; 
import android.support.v7.app.ActionBarActivity; 
import android.widget.ListView; 
import android.widget.Toast; 

import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.ArrayList; 
import java.util.Timer; 
import java.util.TimerTask; 

public class MainActivity extends ActionBarActivity { 

ListView lViewSMS; 

String URL = "http://krith.esy.es/index2.php"; 

JSONParser jsonParser = new JSONParser(); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    lViewSMS = (ListView) findViewById(R.id.listViewSMS); 
    final Handler handler = new Handler(); 
    Timer t = new Timer(); 
    t.scheduleAtFixedRate(new TimerTask() { 

           @Override 
           public void run() { 
            getsms(); 
           } 

          }, 

      0, 

      30000); 

} 
void getsms() { 
    String []r; 
    r=new String[2]; 
    fetchInbox(); 
} 

void fetchInbox(){ 

    final Uri SMS_INBOX = Uri.parse("content://sms/inbox"); 
    Cursor cursor = getContentResolver().query(SMS_INBOX, null,"read=0", null, null); 
    ArrayList sms = new ArrayList(); 

    String read=" "; 
    String body=" "; 
    String[] arr = new String[2]; 
    while (cursor.moveToNext()) {   
     String address = cursor.getString(cursor.getColumnIndex("address")); 
     String person = cursor.getString(cursor.getColumnIndex("person")); 
     String date = cursor.getString(cursor.getColumnIndex("date")); 
     String protocol =cursor.getString(cursor.getColumnIndex("protocol")); 
     read+= cursor.getString(cursor.getColumnIndex("read")); 
     String status = cursor.getString(cursor.getColumnIndex("status")); 
     String type = cursor.getString(cursor.getColumnIndex("type")); 
     String subject = cursor.getString(cursor.getColumnIndex("subject")); 
     body = cursor.getString(cursor.getColumnIndex("body")); 

     sms.add(address+"\n"+person+"\n"+body+"\n"); 
     arr[0] = body; 
     arr[1] = read; 
     if(arr!=null) { 
      AttemptLogin attemptLogin = new AttemptLogin(); 
      attemptLogin.execute(arr[0], arr[1]); 
     } 
     else 
     { 
      AttemptLogin attemptLogin = new AttemptLogin(); 
      attemptLogin.execute("", ""); 
     } 
    } 
} 
class AttemptLogin extends AsyncTask<String,String,JSONObject> { 

    @Override 

    protected void onPreExecute() { 

     super.onPreExecute(); 

    } 

    @Override 

    protected JSONObject doInBackground(String... args) { 



     String msg = args[0]; 
     String st=args[1]; 

     ArrayList params = new ArrayList(); 
     params.add(new BasicNameValuePair("msg", msg)); 
     params.add(new BasicNameValuePair("st", st)); 


     JSONObject json = jsonParser.makeHttpRequest(URL, "POST", params); 


     return json; 

    } 

    protected void onPostExecute(JSONObject result) { 

     try { 
      if (result != null) { 
       Toast.makeText(getApplicationContext(), result.getString("message"), Toast.LENGTH_LONG).show(); 
      } else { 
       Toast.makeText(getApplicationContext(), "Unable to retrieve any data from server", Toast.LENGTH_LONG).show(); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 


    } 

} 

}

+2

當您收到新消息時,使其成爲服務或廣播接收器。 – TruongHieu

+0

使用BroadCastReceiver實現此功能 –

+0

即時通訊新的android.can你請告訴如何做到這一點.. – Krithi

回答

1

在目標(丟失)設備您的應用程序應首先接收短信,然後將其重新發送到另一個號碼。 ,它需要BroadcastReceiver和SMS許可check this tutorial http://androidexample.com/Incomming_SMS_Broadcast_Receiver_-_Android_Example/index.php?view=article_discription&aid=62

+0

謝謝you.i將嘗試this.where包括此行遊標cursor = getContentResolver()。query(SMS_INBOX,null, 「read = 0」,null,null); – Krithi

+0

你不需要,每次新的短信到達應該轉移到另一個號碼,你不能讀取所有的短信息數據庫eveytime –

+0

雅我得到它。我應該每次更新數據庫與整個消息框。只是檢索未讀的短信時需要時從數據庫中獲得。 – Krithi