Thursday, August 11, 2011

Alert Dialog From Service or BroadCastReceiver


Actually we cant use alertdialog on Service, Alert can be done through Activity only, so the better idea is call the activity from service class.

Use this on service class:



   @Override
    public void onReceive(Context context, Intent intent) {

                        Intent trIntent = new Intent("android.intent.action.MAIN");
        trIntent.setClass(context, com.Ss.controls.YourDialog.class);
        trIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        
               context.startActivity(trIntent);              
}

and Create an activity class:


public class YourDialog extends Activity {

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
Context ctx = this;

final AlertDialog.Builder alert = new AlertDialog.Builder(ctx);
   alert.setTitle("Message");
   alert.setMessage("checking");
   alert.setPositiveButton("Accept",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int whichButton)
{

}});
     alert.create();
     alert.show();
}

}



 
© 2011 Android Likes Me | Except as noted, this content is licensed under Creative Commons Attribution 2.5.
For details and restrictions, see the Content License | Recode by Ardhiansyam | Based on Android Developers Blog