2017-03-01 7 views
0

私の電話が紛失した場合、私の電話から未読のSMSを他の電話から読み取る必要があるアプリケーションを開発しています。データベースの未読のSMSをすべて更新しました半分分ですが、アプリを開くときに更新するだけですが、自動的に更新する必要があります。誰でもこの改善のために手伝ってください。私は以下のコードを投稿しました。携帯電話の未読のSMSを別の電話から読み取る方法

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

私はアンドロイドに新しいです。あなたはそれを行う方法を教えてください.. – Krithi

答えて

1

は、ターゲットであなたのアプリは最初のSMSを受信し、別の番号にそれを再送信する必要があり、デバイスを(失いました)。 ブロードキャスト受信者とSMS許可が必要ですcheck this tutorial http://androidexample.com/Incomming_SMS_Broadcast_Receiver_-_Android_Example/index.php?view=article_discription&aid=62

+0

ありがとうございます。私はこの行を含めることができますこのカーソルを含めるにはカーソル= getContentResolver()。クエリ(SMS_INBOX、 "read = 0"、null、null); – Krithi

+0

新しいSMSが到着するたびに別の番号に転送する必要はありません。すべてのSMS DB eveytimeを読む必要はありません –

+0

ya it.iはメッセージボックス全体をデータベースに更新する必要があります。必要に応じてdbから必要です。 – Krithi

関連する問題