0
我正在開發一個應用程序,通知用戶,當任何短信標記爲已讀,即使應用程序沒有運行contentobserver僅插入工程和刪除,但不是更新
我只是創造了一個contentobserver和我註冊它在服務
的問題是,如果新的SMS插入或刪除,但是當標記爲已讀(更新操作)的SMS這是行不通的contentobserver運行
這裏是我的服務代碼
public class Smssendservice extends Service {
private static Timer timer = new Timer();
private Context ctx;
public IBinder onBind(Intent arg0)
{
return null;
}
public void onCreate()
{
super.onCreate();
ctx = this;
startService();
}
private void startService()
{
//timer.scheduleAtFixedRate(new mainTask(), 0, 5000);
Toast.makeText(getApplicationContext(), "Before Register", Toast.LENGTH_SHORT).show();
final Uri SMS_STATUS_URI = Uri.parse("content://sms");
SMSLogger sl= new SMSLogger();
SMSObserver smsSentObserver = new SMSObserver(sl, ctx);
getContentResolver().registerContentObserver(SMS_STATUS_URI, true, smsSentObserver);
Toast.makeText(getApplicationContext(), "After Register", Toast.LENGTH_SHORT).show();
}
}
我在服務
這裏登記我的內容觀察者內容觀察者碼
public class SMSObserver extends ContentObserver
{
SMSLogger smsLogger;
Context context;
public SMSObserver(SMSLogger smsLogger, Context context) {
super(new Handler());
this.context=context;
this.smsLogger = smsLogger;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
smsLogger.querySMS(context);
}
}
最終這裏的短信記錄,我顯示TOAST如果SMS數據改變
public class SMSLogger {
protected void querySMS(Context context) {
Uri uriSMS = Uri.parse("content://sms/");
Cursor cur = context.getContentResolver().query(uriSMS, null, null, null, null);
/* cur.moveToNext(); // this will make it point to the first record, which is the last SMS sent
String body = cur.getString(cur.getColumnIndex("body")); //content of sms
String add = cur.getString(cur.getColumnIndex("address")); //phone num
String time = cur.getString(cur.getColumnIndex("date")); //date
String protocol = cur.getString(cur.getColumnIndex("protocol")); //protocol*/
Toast.makeText(context, "Data Changed CHECK SMS" , Toast.LENGTH_SHORT).show();
/*logging action HERE...*/
}
}
它顯示此消息「數據更改檢查短信」,如果新的短信插入或短信刪除,但在更新的情況下吐司不出現。任何線索?
應該「content:// sms」通知這樣的更新嗎?你在哪裏讀過的? – pskink
它聽取插入並刪除爲什麼它不聽更新呢? 確實知道如何收聽更新動作?我應該使用短信/收件箱嗎? –
請參閱https://developer.android.com/reference/android/provider/Telephony.Sms.Inbox.html – pskink