栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android开机启动广播

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android开机启动广播

Android开机启动广播




  • 理论概述
  • 核心代码




第一章 理论概述 第01节 基础说明
1、开机启动的过程当中, 定义开机启动广播。

2、接收到开机启动广播之后, 可以开启 Service 
			A. 低版本实现 Android8.0 之前的实现
			B. 高版本实现 Android8.0 之后的实现
			
3、接收到开机启动广播之后, 可以开启 Activity
			A. 低版本实现 Android8.0 之前的实现
			B. 高版本实现 Android8.0 之后的实现



第02节 基础步骤
1. 定义清单文件
		A. 三种权限(开机广播、后台服务、后台Activity)
		B. 定义三类(Activity、Service、Receiver)


2. 定义广播接收者 Receiver
		A. 重写方法 onReceive()  
		B. 分版本开启  Service
		C. 分版本开启  Activity


3. 定义服务 Service
		A. 重写方法 onBind()  
		B. 重写方法 onCreate()  
		C. 重写方法 onStartCommand()


4. 定义 Activity
		A. 重写方法 onCreate()




第03节 效果图

开机启动Activity 高版本效果



开机启动Activity 低版本效果



开机启动Service高版本效果



开机启动Service低版本效果




第二章 核心方法 第01节 清单文件



    
    

    
    

    
    
    

    
        
        
            
                
                
            
        
        
        
            
                
                
            
        
        
        
    


1、 权限定义

2、组件定义




第02节 Activity
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "hello";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "onCreate: hello ===>MainActivity启动了");
    }
}

1、成员变量 主要是用于打印的标记 TAG


2、成员方法 主要是 onCreate 重写的方法




第03节 Service
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;


public class HelloService extends Service {

    private static final String TAG = "hello";
    public static final String CHANNEL_ID_STRING = "service_01";
    private Notification notification;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Log.i(TAG, "onCreate: ...服务创建...");

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand: start....服务启动...");
        //下面是判断 Android8.0 以后的服务,设置为后台服务的操作
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel mChannel = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            mChannel = new NotificationChannel(CHANNEL_ID_STRING, 
                                               getString(R.string.app_name),
                                               NotificationManager.importANCE_LOW);
            manager.createNotificationChannel(mChannel);
            notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID_STRING).build();
            //注意 startForeground(1, notification);中不能为0,不然会出现如下问题
            //Context.startForegroundService() did not then call Service.startForeground()
            startForeground(1, notification);
        }
        return super.onStartCommand(intent, flags, startId);
    }
}

1、成员变量

​ A. 用于打印的标记 TAG

​ B. 用于通知的标记 CHANNEL_ID_STRING

​ C. 用于通知的对象 Notification


2、成员方法

​ A. 绑定方法 onBind

​ B. 创建方法 onCreate

​ C. 开启方法 onStartCommand




第04节 Receiver
import static android.content.Context.NOTIFICATION_SERVICE;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;


public class BootCompleteReceiver extends BroadcastReceiver {
    private static final String TAG = "hello";
    private static final String BOOT_ACTION = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Boot Complete 开机启动的广播接收者", Toast.LENGTH_LONG).show();
        Log.i(TAG, "onReceive: Boot Complete 开机启动的广播接收者");
        //开机的过程当中,启动 Activity的操作,判断当前启动的动作是开机启动的
        if (BOOT_ACTION.equals(intent.getAction())) {
            //开启Activity
            openActivity(context);
            //开启Service
            //openService(context);
        }
    }

    
    public void openService(Context context) {
        Intent newIntent = new Intent(context,HelloService.class);
        //判断当前编译的版本是否高于等于 Android8.0 或 26 以上的版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(newIntent);
        } else {
            context.startService(newIntent);
        }
        Log.i(TAG, "openService: 启动Service");
    }

    
    public void openActivity(Context context) {
        //判断当前编译的版本是否高于等于 Android8.0 或 26 以上的版本
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            startActivityVersionHeight(context);
        }else{
            startActivityVersionLower(context);
        }
        Log.i(TAG, "openActivity: 启动Activity");
    }

    
    public void startActivityVersionLower(Context context) {
        Intent myIntent = new Intent(context,MainActivity.class);
        myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(myIntent);
    }

    
    public void startActivityVersionHeight(Context context) {

        Intent intent1 = new Intent(context,MainActivity.class);
        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(context, 0,
                intent1, PendingIntent.FLAG_UPDATe_CURRENT);

        String channelID = "my_channel_ID";
        String channelNAME = "my_channel_NAME";
        int level = NotificationManager.importANCE_HIGH;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationManager manager = (NotificationManager)
                    context.getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
            manager.createNotificationChannel(channel);
        }
        NotificationCompat.Builder notification =
                new NotificationCompat.Builder(context, channelID)
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setContentTitle("Start Activity")
                        .setContentText("click me")
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .setCategory(NotificationCompat.CATEGORY_CALL)
                        .setAutoCancel(true)
                        .setFullScreenIntent(fullScreenPendingIntent, true);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(100, notification.build());
    }
}

1、成员变量

​ A. 打印日志的标记 TAG

​ B. 开机启动的广播 BOOT_ACTION


2、成员方法

​ A. 接收广播的方法 onReceive

​ B. 开启服务的方法 openService

​ C. 开启Activity的方法 openActivity

​ D. 低版本启动 Activity的实现 startActivityVersionLower

​ E. 高版本启动 Activity的实现 startActivityVersionHeight







转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/458649.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号