我正在开发一个应用程序,如果我的手机丢失了,我需要从其他手机上读取手机中未读的短信。我试图每半分钟更新数据库中未读的短信。但是只有当我打开时它才会更新app.but我需要自动更新它。任何人都可以帮助我改进这个。或者建议其他一些方法来做到这一点??我在下面发布了我的代码。
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();
}
}
}
}
答案 0 :(得分:1)
在目标(丢失)设备中,您的应用应首先接收短信,然后将其重新发送到另一个号码。 它需要BroadcastReceiver和SMS Permission check this tutorial http://androidexample.com/Incomming_SMS_Broadcast_Receiver_-_Android_Example/index.php?view=article_discription&aid=62