[英]display alert dialog in broatcastReceiver
我想開發一個接收短信並顯示警報對話框的應用程序,該對話框需要用戶許可並在手機中搜索特定聯系人並發送回復消息。 但是如果不使用 Toast 正在運行的對話框,我就無法顯示警報對話框。請幫助我。
public void onReceive( Context context, Intent intent ) {
// Get SMS map from Intent
Bundle extras = intent.getExtras();
String messages = "";
if ( extras != null ) {
// Get received SMS array
Object[] smsExtra = (Object[]) extras.get( "pdus" );
// Get ContentResolver object for pushing encrypted SMS to incoming folder
ContentResolver contentResolver = context.getContentResolver();
for ( int i = 0; i < smsExtra.length; ++i ) {
SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
String body = sms.getMessageBody().toString();
String address = sms.getOriginatingAddress();
messages += "SMS from " + address + " :\n";
messages += body + "\n";
// Here you can add any your code to work with incoming SMS
// I added encrypting of all received SMS
}
// Display SMS message
Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();
AlertDialog.Builder dialog=new AlertDialog.Builder(context);
dialog.setTitle("You've Requsted msg");
dialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
} );
dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
dialog.show();
}
}
// WARNING!!!
// If you uncomment next line then received SMS will not be put to incoming.
// Be careful!
// this.abortBroadcast();
}
一個更好的主意是創建一個 ACtivity 並將其主題設置為 Dialog.. 然后使用 Context 來啟動這個 Activity 使用context.startACtivity(intent);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.