Is possible to Show Dialog box in Service [android

2019-08-29 09:59发布

I'm a new to android, so I'm trying to develop an application that shows a notification to the user after specific time, I don't know: Is it possible to use a dialog box inside Service ! or not !...

My Goal is: to ask him a question after specific time, as shown below in my code,

import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyAlarmService extends Service {

    @Override
    public void onCreate() {
        Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG)
                .show();
    }

    @Override
    public IBinder onBind(Intent intent) {
        Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG)
                .show();
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG)
                .show();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG)
                .show();
        // For Notification
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Conformation");
        alertDialog
                .setMessage("Are you sure you want to Expand Report Region?");
        alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Sending a Message to server that the plaintiff wants to
                // expand report region
            }
        });
        alertDialog.setButton2("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Do nothing
            }
        });
    }
}

Also I don't know shall I use Service or BroadcastReceiver ??

any suggestion, thanks in advance =)

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-29 10:02

Its always better to use Broadcast receiver and Service both. Register intents in broadcast receiver and from broadcast receiver redirect request to Service.

Its possible to show dialog box from service. only thing you need is an Activity.

In short your application will have one Activity , One Service and one Broadcast receiver. Your broadcast receiver will catch the intent which will redirect to a Service and then your service will invoke activity which shows dialog box.

BroadCast Receiver -- > Service --> Activity

查看更多
▲ chillily
3楼-- · 2019-08-29 10:03

BroadCast Receiver -- > Service --> Activity this sequence is good. For displaying alert dialog without pressing any button call method, showDialog() in Activity's onCreate() method.

查看更多
登录 后发表回答