android service 弹出,Android在Service中弹出对话框二

该博客介绍了如何在Android应用中创建一个定时弹窗服务,并针对Android 8.0及以上版本的后台限制进行了适配。通过Service和Handler实现定时任务,展示系统警告对话框,并检查并请求SYSTEM_ALERT_WINDOW权限以确保在Android 8.0以上版本能正常弹窗。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一篇我也写了一篇弹窗的,但是经过测试,Android8.0之后用不了,所以改一下

Myservice.class

package com.nf.service;

import android.app.AlertDialog;

import android.app.AliasActivity;

import android.app.Dialog;

import android.app.Service;

import android.content.DialogInterface;

import android.content.Intent;

import android.os.Binder;

import android.os.Build;

import android.os.Handler;

import android.os.IBinder;

import android.os.Looper;

import android.util.Log;

import android.view.WindowManager;

import android.widget.Toast;

import java.sql.Time;

import java.util.Objects;

import java.util.Timer;

import java.util.TimerTask;

public class MyService extends Service {

// 全局声明Handler

private Handler mHandler;

public MyService() {

}

// 创建一个服务

@Override

public void onCreate() {

super.onCreate();

mHandler = new Handler(Looper.getMainLooper());

}

// 服务启动

@Override

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

Log.i("MyServer", "服务启动了");

Timer timer = new Timer();

int n = 5 * 60 * 10;

timer.scheduleAtFixedRate(new TimerTask() {

@Override

public void run() {

// new Thread(new Runnable() {

// @Override

// public void run() {

// Looper.prepare();

// // TODO: 2019/11/7

// AlertDialog.Builder builder = new AlertDialog.Builder(MyApp.getContext());

// builder.setTitle("提示");

// builder.setMessage("已经过去五分钟了\n且行且珍惜");

// builder.setNegativeButton("明白了", null);

// Dialog dialog = builder.create();

// dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

// dialog.show();

// Looper.loop();

// }

// }).start();

mHandler.post(new Runnable() {

@Override

public void run() {

// AlertDialog.Builder builder = new AlertDialog.Builder(MyApp.getContext());

// builder.setTitle("提示");

// builder.setMessage("已经过去五分钟了\n且行且珍惜");

// builder.setNegativeButton("明白了", null);

// Dialog dialog = builder.create();

// dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

// dialog.show();

Log.i("MyServer", "服务启动了");

showDialog();

}

});

Log.i("MyServer", "五分钟了");

}

}, 10000, n);

return super.onStartCommand(intent, flags, startId);

}

private void showDialog() {

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext())

.setIcon(android.R.drawable.ic_dialog_info)

.setTitle("提示")

.setMessage("已经五分钟过去了\n 时间不等人")

.setPositiveButton("确定",

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,

int whichButton) {

}

});

AlertDialog dialog = builder.create();

//设置点击其他地方不可取消此 Dialog

dialog.setCancelable(false);

dialog.setCanceledOnTouchOutside(false);

//8.0系统加强后台管理,禁止在其他应用和窗口弹提醒弹窗,如果要弹,必须使用TYPE_APPLICATION_OVERLAY,否则弹不出

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

Objects.requireNonNull(dialog.getWindow()).setType((WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY));

}else {

Objects.requireNonNull(dialog.getWindow()).setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));

}

dialog.show();

}

// 服务销毁

@Override

public void onDestroy() {

Log.i("MyServer", "服务死了");

super.onDestroy();

}

@Override

public IBinder onBind(Intent intent) {

// TODO: Return the communication channel to the service.

return new MyBinder();

}

class MyBinder extends Binder {

/**

* 获取Service的方法 * @return 返回PlayerService

*/

public MyService getService() {

return MyService.this;

}

}

}

MainActivty.class

package com.nf.service;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;

import android.content.Intent;

import android.content.ServiceConnection;

import android.net.Uri;

import android.os.Build;

import android.os.Bundle;

import android.os.IBinder;

import android.provider.Settings;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

public Intent intent;

ServiceConnection serviceConnection = new ServiceConnection() {

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

// 建立连接

// 获取服务操作对象

MyService.MyBinder binder = (MyService.MyBinder) service;

binder.getService();//获取到Service

}

@Override

public void onServiceDisconnected(ComponentName name) {

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

intent = new Intent(this, MyService.class);

startService(intent);//启动服务

checkMyPermission();

}

@Override

protected void onDestroy() {

super.onDestroy();

// stopService(intent);

// 程序销毁时,退出服务

}

private void checkMyPermission() {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

if (!Settings.canDrawOverlays(this)) {

Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,

Uri.parse("package:" + getPackageName()));

startActivityForResult(intent, 1);

}

}

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 1) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

if (Settings.canDrawOverlays(this)) {

Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();

} else {

// SYSTEM_ALERT_WINDOW permission not granted...

Toast.makeText(this, "未被授予权限,相关功能不可用", Toast.LENGTH_SHORT).show();

}

}

}

}

}

AndroidManifest.xml

android:name=".MyService"

android:enabled="true"

android:exported="true">

急着去上班,不做解释了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值