我正在开发一个应用程序,如果我的电话丢失了,我需要从我的电话读取其他电话的未读短信。我试图更新数据库中未读的短信半分钟。但只有当我打开应用程序时才更新。但我需要自动更新它。任何人都可以帮助我改善这一点。或者建议采取其他方式来做这件事。我已经在下面发布了我的代码。如何从另一个电话读取一个电话的未读短信
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();
}
}
}
}
当您收到新消息时,使其成为服务或广播接收器。 – TruongHieu
使用BroadCastReceiver实现此功能 –
即时通讯新的android.can你请告诉如何做到这一点.. – Krithi