android 消息提醒

1.创建 MyBackgroundService.java  继承 Service

public class MyBackgroundService extends Service {
   
    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("业务服务", "开起业务服务");
        //调用服务后在页面手机上创建一个通知消息。
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel channel = null;
            channel = new NotificationChannel("100", getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
            Notification notification = new Notification.Builder(getApplicationContext(), "100")
                    .setVisibility(Notification.VISIBILITY_PRIVATE).build();
            startForeground(100, notification);
        }

    }
    @Override
    public int onStartCommand(Intent intent,int flsgs,int startId){
        timer.schedule(task, 60000, 60000);    //定时执行代码
        return  START_STICKY;
    }



    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
//构造消息(在需要发送消息的方法中调用方法)
private void sendMessgae(String my_channel_ID,String my_channel_NAME,String msgContext,int notifyid,String msgType){
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);
    String channelId = createNotificationChannel(my_channel_ID, my_channel_NAME, NotificationManager.IMPORTANCE_HIGH);
    NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(), channelId)
            .setContentTitle("管理系统")
            .setContentText(msgContext)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.newlogo)
            .setAutoCancel(true);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());
    if(notification !=null){
        notificationManager.notify(notifyid,notification.build());
        savemsg(my_channel_ID+"",msgContext,msgType);
    }
}

//消息设置
private String createNotificationChannel(String channelID, String channelNAME, int level) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
        //开启震动
        channel.enableVibration(true);
        //设置锁屏提示
        channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
        manager.createNotificationChannel(channel);
        return channelID;
    } else {
        return null;
    }
}

2.创建 DaemonService.java 守护服务

public class DaemonService extends Service {
        private final static String TAG = DaemonService.class.getSimpleName();

        @Override
        public void onCreate() {
            super.onCreate();
            Log.i("测试开启服务", "开始守护服务");
        }

        @Override
        public int onStartCommand(Intent intent,int flags,int startId){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(new Intent(this, MyBackgroundService.class));
            } else {
                startService(new Intent(this, MyBackgroundService.class));
            }
            return START_STICKY;
        }
}

 3.在 AndroidManifest.xml 中注册服务

<service android:name=".tools.DaemonService"
    android:process=":daemon" />

<service
    android:name=".tools.MyBackgroundService"
    android:enabled="true"
    android:exported="false"
    android:stopWithTask="false" />

手动关闭应用程序后后台进程一直存在。

  • 16
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中,有两种常用的消息提示框可以使用:Toast提示框和AlertDialog。这两种提示框在功能和使用方式上有所不同。 1. Toast提示框是一种简单的消息提示框,用于向用户显示一条临时性的消息。它的代码示例如下: Toast.makeText(MainActivity.this, "有空输入!\n请重新输入!", Toast.LENGTH_SHORT).show(); 这段代码会在底部弹出一个短暂显示的提示框,其中包含有关输入错误的消息。Toast提示框适用于简单的文本提示,对于输入错误等情况较为合适。 2. AlertDialog是一种更为复杂和功能丰富的提示框,可以用于实现多种功能,例如提示说明、单选框、复选框甚至登录功能。AlertDialog可以通过编写XML文件来实现更多的功能和更好的界面。下面是一个使用AlertDialog的示例代码: Handler handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { @Override public void run() { //放在UI线程弹AlertDialog的代码 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("提示") .setMessage("有空输入!\n请重新输入!") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //确定按钮的点击事件处理 } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //取消按钮的点击事件处理 } }) .show(); } }); 这段代码会创建一个AlertDialog并在UI线程中显示出来。其中,通过AlertDialog.Builder来构建对话框的内容,可以设置标题、消息内容以及确定和取消按钮的点击事件。通过调用show()方法来显示对话框。 总结来说,Toast提示框适用于简单的文本提示,而AlertDialog则可以实现更多的功能和更好的界面。具体选择哪种消息提示框取决于实际需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值