Show Dialog using Service

Hi ,

In this post lets try to display a dialog box by using a service .
It's a common problem we face while and most of the time we end up by getting Windows Bad Token exception ... as we cannot use service object to display a dialog , So what's the turn around ..


Hears the solution ..

Create an Activity and display a dialog using it , but while defining an activity in Manifest file just set the theme as dialog box .


Chalo lets C how actually its done ..



Hear's my main activity , I am using it to start the service ..

package com.android.servicedialog;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity  implements OnClickListener{

Button btnStart;
Button btnStop;
private Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


btnStart = (Button) findViewById(R.id.btnStartService);
btnStop = (Button) findViewById(R.id.btnStopService);

btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
intent = new Intent(MainActivity.this , DemoService.class);


}

@Override
public void onClick(View v) {

if (v.getId() == R.id.btnStartService) {

MainActivity.this.startService(intent);


}else if(v .getId() == R.id.btnStopService){

MainActivity.this.stopService(intent);

}
}


}





Here come's the service code ...
A simple service which starts a activity on its onStart method



package com.android.servicedialog;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class DemoService extends Service {

@Override
public IBinder onBind(Intent intent) {

return null;
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {

intent = new Intent(DemoService.this , DialogActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
super.onDestroy();
}



}



This is the Activity code .. from here we are going to show the Dialog box . 

package com.android.servicedialog;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class DialogActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Utils.showAlertDialogBox("Sample Msg from Service", DialogActivity.this);
}

}


This is the Util's class .. here we have created a Custom Dialog Box 


package com.android.servicedialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class Utils {
/*
 * Display alert dialog box as per the parameter
 */
public static void showAlertDialogBox(String msg, final Activity activity) {
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);

TextView title = (TextView) dialog.findViewById(R.id.textTitle);
title.setText("BreakDown!!");

TextView txtSubTitle = (TextView) dialog
.findViewById(R.id.textSubTitle);
txtSubTitle.setText(msg);

Button dialogButton = (Button) dialog.findViewById(R.id.btnOk);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
activity.finish();
}
});

dialog.show();
}
}


Here comes the MOST IMP part .. The manifest file .. Here we have defined the activity and then set the style to Dialog ..


        < service
            android:name="com.android.servicedialog.DemoService">
         
        < /service>
     
       < activity
            android:name="com.android.servicedialog.DialogActivity"
            android:theme="@style/Theme.AppCompat.CompactMenu.Dialog">
         
        < /activity>
   




No comments:

Post a Comment