[英]Android SMS intent stuck with the same body and same recepient
因此,我的android應用程序會檢測到位置更改的時間,然后通知用戶,並使他采取行動(撥打電話或發送短信)。
SMS發送到一個已保存的號碼,其正文為"I'm at " + fullAddress
private NotificationCompat.Builder buildNormal(CharSequence pTitle,String fullAddress) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
Intent in = new Intent(this,MainActivity.class);
PendingIntent pMainIntent = PendingIntent.getActivity(this, 0,
in, 0);
if(getSavedDataString("gNumber")!=null)
{
String url = getSavedDataString("gNumber");
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
//Intent smsIntent = new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto:"
//+ Uri.encode(getSavedDataString("gNumber").substring(4))));
//intent.setData());
//startActivity(intent);
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
//smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", getSavedDataString("gNumber").substring(4));
smsIntent.putExtra("sms_body","I'm at " + fullAddress);
smsIntent.setData(Uri.parse("smsto:"
+ Uri.encode(getSavedDataString("gNumber").substring(4))));
PendingIntent psmsIntent = PendingIntent.getActivity(this, 0,
smsIntent, 0);
builder.addAction(android.R.drawable.ic_menu_call, "Call", pIntent);
builder.addAction(android.R.drawable.sym_action_email, "Send SMS", psmsIntent);
}
else
{
builder.addAction(0, "Choose Guardian", pMainIntent);
}
builder.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL);
// set the shown date
builder.setWhen(System.currentTimeMillis());
// the title of the notification
builder.setContentTitle(pTitle);
// set the text for pre API 16 devices
builder.setContentText(pTitle);
// set the action for clicking the notification
builder.setContentIntent(pMainIntent);
// set the notifications icon
builder.setSmallIcon(R.drawable.ic_home);
//builder.setSound(android.)
// set the small ticker text which runs in the tray for a few seconds
builder.setTicker("Location Change Alert");
// set the priority for API 16 devices
//builder.setVibrate(pattern)
builder.setPriority(Notification.PRIORITY_DEFAULT);
return builder;
}
它在此處顯示了向用戶顯示的通知包含兩個呼叫或發送消息的操作,並將其發送到已保存的號碼gNumber
問題是在我按操作發送短信然后不發送該消息而丟棄該消息之后,也將其從草稿中刪除。 然后應用程序檢測到另一個位置更改,因此它使用不同的fullAddress發送了一個不同的通知,其意圖仍然停留在同一文本正文中!!
我還嘗試更改接收者,它也卡在了舊接收者上。 我必須重新啟動設備或發送我曾經丟棄的消息。
我也嘗試將ACTION_VIEW
更改為ACTION_SEND
或ACTION_SENDTO
但徒勞無功。
我想知道是否有解決此意圖的方法,而不是完全更改此意圖並使用SMSManager,而使該意圖卡在同一主體和收件人上
請幫助。
當您的應用程序請求PendingIntent
,系統會代表您的應用程序保留令牌,以執行操作,就好像您的應用程序實際上正在執行該操作一樣。 這樣,即使您的應用程序被殺死,接收到PendingIntent
任何進程仍可以繼續執行。
創建這些令牌時,將記錄某些信息,例如,操作,操作等。如果您的應用程序請求另一個具有相同信息的PendingIntent
,則將返回相同的令牌。 現在,用於確定它們是否相同的信息不包括Intent
本身所具有的附加功能。 因此,當您的應用僅使用不同的Intent
附加信息請求相同的SMS操作時,您將一遍又一遍地獲得與原始附加信息相同的令牌, 除非您傳遞一個標志來表示不同。
至於標志之間的區別: FLAG_CANCEL_CURRENT
“確保只有給定新數據的實體才能啟動它。如果這不是問題,請考慮FLAG_UPDATE_CURRENT
。” 就您而言,我認為這不是問題,因此FLAG_UPDATE_CURRENT
應該足夠了。 如果有問題,請使用FLAG_CANCEL_CURRENT
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.