display alert dialog in broatcastReceiver

Go To StackoverFlow.com

0

I would like to develop an app that receives sms and display alert dialog box that takes user permission and search the mobile for particular contact and send reply message. But i can't display alert dialog without using dialog box the Toast is working.please help me.

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(); 
}
2012-04-04 06:14
by venkyMCA


1

A better idea is to Create an ACtivity and set its Theme to be Dialog.. And then use Context to start this Activity using context.startACtivity(intent);

2012-04-04 06:19
by ngesh
Ads