在broadcastReceiver中显示警告对话框

时间:2012-04-04 06:14:36

标签: android

我想开发一个接收短信和显示警告对话框的应用程序,该对话框获取用户权限并在移动设备上搜索特定联系人并发送回复消息。但是我不能在没有使用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(); 
}

1 个答案:

答案 0 :(得分:1)

更好的想法是创建一个ACtivity并将其主题设置为Dialog ..然后使用Context以context.startACtivity(intent);启动此活动