zoukankan      html  css  js  c++  java
  • Android 开机启动通知

    效果图:

    学习:

    1.静态注册实现开机启动

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
            <receiver android:name="com.example.alarmmanagerdemo.AlarmReceiver" >
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
            </receiver>

    2.通知的创建

    step1:需要一个NotificationManager来对通知进行管理

     NotificationManager manager = (NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE); 

    step2:创建一个Notification对象,这个对象用于存储通知所需的各种信息。

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            builder.setContentTitle("Mission Of Everyday").setContentText("Remeber Vocabulary, Exercise, Speaking English").setSmallIcon(R.drawable.sun).setDefaults(Notification.DEFAULT_ALL).setAutoCancel(true).setSubText("                          For Dream!");

    step3:调用NotificationManager的notify()让通知显示出来。

    manager.notify(1, builder.build());

    源码:

    import android.app.Notification;
    import android.app.NotificationManager;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.support.v4.app.NotificationCompat;
    import android.widget.Toast;
    
    
    public class AlarmReceiver extends BroadcastReceiver {
        private NotificationManager manager;
        @Override
        public void onReceive(Context context, Intent intent) {
            //Toast.makeText(context, "闂归搩鍝嶄簡, 鍙�互鍋氱偣浜嬫儏浜唦~", Toast.LENGTH_LONG).show();
            Toast.makeText(context, "Boot Complete", Toast.LENGTH_LONG).show();
            manager = (NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
            
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            builder.setContentTitle("Mission Of Everyday").setContentText("Remeber Vocabulary, Exercise, Speaking English").setSmallIcon(R.drawable.sun).setDefaults(Notification.DEFAULT_ALL).setAutoCancel(true).setSubText("                          For Dream!");
            manager.notify(1, builder.build());
        }
    
    }
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.alarmmanagerdemo"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="18" />
    
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/icon"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.alarmmanagerdemo.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <receiver android:name="com.example.alarmmanagerdemo.AlarmReceiver" >
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
            </receiver>
        </application>
    
    </manifest>

    Demo下载地址:http://pan.baidu.com/s/1hrWY5kK

    参考:1.http://www.runoob.com/w3cnote/android-tutorial-notification.html

             2.《第一行代码》

  • 相关阅读:
    iOS 网络编程:socket
    Zsh和Bash,究竟有何不同
    关于微软win10 2004的更新以及wsl2 Ubuntu18.04安装
    C# 委托事件机制 订阅发布
    关于Docker理念和安装,对Visual Studio2019自带生成的DockerFile配置,以及Docker镜像的发布与拉取
    div弹框
    vs分页
    ado显示下拉
    SQL server动态拼接存储过程分页
    退役
  • 原文地址:https://www.cnblogs.com/ZP-Better/p/5357300.html
Copyright © 2011-2022 走看看