Android Service中AlertDialog全局对话框使用(1)

一、startService 服务的使用
1、创建一个MyService服务

public class MyService extends Service {
    private static final String TAG = "MyService";
    private Context context;
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind()");
        return null;
    }
    @Override
    public void onCreate() {
        Log.e(TAG, "onCreate()");     
        context = this;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand()");
        showDialog();
        return super.onStartCommand(intent, flags, startId);
    }

    // 调用startService方法启动Service时调用该方法
    @Override
    public void onStart(Intent intent, int startId) {
        Log.e(TAG, "onStart()");
    }
    @Override
    public void onDestroy() {
        Log.e(TAG, "onDestroy()");       
    }
    public void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
        View view = View.inflate(context, R.layout.activity_dialog_view, null);
        AlertDialog  dialog = builder.create();
        dialog.setView(view);
        //8.0系统加强后台管理,禁止在其他应用和窗口弹提醒弹窗,如果要弹,必须使用TYPE_APPLICATION_OVERLAY
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0新特性
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY - 1);
        } else {
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
        }
        dialog.show();
        Window dialogWindow = dialog.getWindow();//获取window对象
        dialogWindow.setGravity(Gravity.CENTER);//设置对话框位置
    }
}

被启动的服务的生命周期:如果一个Service被某个Activity 调用 Context.startService 方法启动,那么不管是否有Activity使用bindService绑定或unbindService解除绑定到该Service,该Service都在后台运行。如果一个Service被startService 方法多次启动,那么onCreate方法只会调用一次,onStart将会被调用多次(对应调用startService的次数),并且系统只会创建Service的一个实例(因此你应该知道只需要一次stopService调用)。该Service将会一直在后台运行,而不管对应程序的Activity是否在运行,直到被调用stopService,或自身的stopSelf方法。当然如果系统资源不足,android系统也可能结束服务
生命周期方法说明
onStartCommand()
当另一个组件(如 Activity)通过调用 startService() 请求启动服务时,系统将调用此方法。一旦执行此方法,服务即会启动并可在后台无限期运行。 如果您实现此方法,则在服务工作完成后,需要由您通过调用 stopSelf() 或 stopService() 来停止服务。(如果您只想提供绑定,则无需实现此方法。)
onCreate()
首次创建服务时,系统将调用此方法来执行一次性设置程序(在调用 onStartCommand() 或 onBind() 之前)。如果服务已在运行,则不会调用此方法。
onDestroy()
当服务不再使用且将被销毁时,系统将调用此方法。服务应该实现此方法来清理所有资源,如线程、注册的侦听器、接收器等。 这是服务接收的最后一个调用

2019-12-02 22:16:47.294 16179-16179/com.example.myapplication E/MyService: onCreate()
2019-12-02 22:16:47.294 16179-16179/com.example.myapplication E/MyService: onStartCommand()
2019-12-02 22:16:47.401 16179-16179/com.example.myapplication E/MyService: onStart()
2019-12-02 22:16:51.362 16179-16179/com.example.myapplication E/MyService: onStartCommand()
2019-12-02 22:16:51.443 16179-16179/com.example.myapplication E/MyService: onStart()
2019-12-02 22:16:57.178 16179-16179/com.example.myapplication E/MyService: onStartCommand()
2019-12-02 22:16:57.272 16179-16179/com.example.myapplication E/MyService: onStart()

2、在AndroidManifest.xml中定义一个服务

  <service android:name="com.example.myapplication.MyService" >
  </service>

3、启动服务

 Intent intentOne = new Intent(MainActivity.this, MyService.class);
                        startService(intentOne);

二、AlertDialog使用
1、自定义AlertDialog的布局文件activity_dialog_view

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:background="#FF6347"
    android:gravity="center"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:text="你使用的卡不存在"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你使用的"/>
    <TextView
        android:layout_width="wrap_content"
        android:textSize="20dp"
        android:layout_height="wrap_content"
        android:textColor="#FF0000"
        android:text="【我知道的】"/>
</LinearLayout>

2、Service中AlertDialog全局对话框需要添加的权限

 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />

3、在代码中使用

 public void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
        View view = View.inflate(context, R.layout.activity_dialog_view, null);
        AlertDialog  dialog = builder.create();
        dialog.setView(view);
        //8.0系统加强后台管理,禁止在其他应用和窗口弹提醒弹窗,如果要弹,必须使用TYPE_APPLICATION_OVERLAY
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0新特性
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY - 1);
        } else {
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
        }
        dialog.show();
        Window dialogWindow = dialog.getWindow();//获取window对象
        dialogWindow.setGravity(Gravity.CENTER);//设置对话框位置
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Service使用AlertDialog需要注意一些问题,因为Service并没有UI界面,所以需要在Service创建一个Handler来处理AlertDialog的显示。 以下是在Service使用AlertDialog的步骤: 1. 在Service创建一个Handler,用于处理AlertDialog的显示。 ```java private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case SHOW_DIALOG: showDialog(); break; case DISMISS_DIALOG: dismissDialog(); break; default: break; } } }; ``` 2. 在Service创建一个AlertDialog,并在需要显示AlertDialog时发送一个 Message 给 Handler。 ```java private void showDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("提示"); builder.setMessage("这是一个AlertDialog"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击确定按钮后的操作 } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击取消按钮后的操作 } }); mAlertDialog = builder.create(); mAlertDialog.show(); } private void dismissDialog() { if (mAlertDialog != null) { mAlertDialog.dismiss(); } } ``` 3. 在Service接收来自其他组件的指令,并根据指令发送不同的 Message 给 Handler。 ```java @Override public int onStartCommand(Intent intent, int flags, int startId) { int command = intent.getIntExtra("command", 0); switch (command) { case SHOW_DIALOG: mHandler.sendEmptyMessage(SHOW_DIALOG); break; case DISMISS_DIALOG: mHandler.sendEmptyMessage(DISMISS_DIALOG); break; default: break; } return super.onStartCommand(intent, flags, startId); } ``` 通过以上步骤,在Service就可以使用AlertDialog了。需要注意的是,在Service使用AlertDialog时,需要在AndroidManifest.xml声明权限 android.permission.SYSTEM_ALERT_WINDOW。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值