该课时开始讲解GlobalKey一键启动程序,一键启动的过程如下: a.对于GlobalKey,系统会根据global_key.xml文件决定发送消息给那个组件 b.APP应该注册广播消息的接收者 1.编写一个BroadCastReceiver派生类,实现消息的处理函数 2.注册派生类 c.在该组件中启动APP 首先我们实现a,b两点,先写出一个能接收广播消息的应用程序,、
APP应用程序我们先来实现上述的b,然后再实现a。
APP修改我们在之前的APP_0001_LEDDemp-V3上进行修改,使用AS进入该工程,点击: 弹出如下窗口:
给需要创建的类命名为MyBroadcastReceiver,可以看到生成了一个新的文件,其内容为下:
public class MyBroadcastReceiver {
}
我们让该类继承于BroadcastReceiver,然后我们需要复写其成员onReceive,简单修改如下:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Get BroadcasstReceiver",Toast.LENGTH_SHORT).show();
}
}
当接收到广播的时候会执行onReceive函数,Toast.makeText(context,“Get BroadcasstReceiver”,Toast.LENGTH_SHORT).show()为简单的显示一个提示框,其内容为"Get BroadcasstReceiver",下面我们修改app-> manifests-> AndroidManifest.xml文件,注册该APP为一个静态广播接收者,在
其中的android:name="android.intent.action.GLOBAL_BUTTON"代表只有接收到GLOBAL_BUTTON这种广播,才会执行onReceive方法。
APP系统签名发送一个ndroid.intent.action.GLOBAL_BUTTON的广播,是需要ACTION_MANAGE_WRITE_SETTINGS权限的, 在安卓6.0以后获取ACTION_MANAGE_WRITE_SETTINGS权限,需要通过用户进行确认,在onCreate中,我们添加如下获取权限代码:
checkBoxLed4 = (CheckBox)findViewById(R.id.LED4);
button.setOnClickListener(new MyButtonListener());
+ //申请android.permission.WRITE_SETTINGS权限的方式
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ //如果当前平台版本大于23平台
+ if (!Settings.System.canWrite(this)) {
+ //如果没有修改系统的权限这请求修改系统的权限
+ Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
+ intent.setData(Uri.parse("package:" + getPackageName()));
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ startActivityForResult(intent, 0);
+ } else {
+ //有了权限,你要做什么呢?具体的动作
+ // processShow();
+ }
+ }
这样就能跳转到用户设置权限界面了,但是,我们运行APP的时候会发现,虽然跳转到了用户界面,选择的拖动图标,是灰色的,依然无法选择,因为在android 6.0及以后,WRITE_SETTINGS权限的保护等级已经由原来的dangerous升级为signature,这意味着我们的APP需要用系统签名或者成为系统预装软件才能够申请此权限,并且还需要提示用户跳转到修改系统的设置界面去授予此权限
那么下面我们就设置让我们的APP使用系统签名,获取系统权限,首先我们在linux中创建文件夹sigAPK,并且在该目录下创建文件keytool-importkeypair,编写内容如下(该为一个脚本文件):
#! /bin/bash
#
# This file is part of keytool-importkeypair.
#
# keytool-importkeypair is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# keytool-importkeypair is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with keytool-importkeypair. If not, see
# .
#
DEFAULT_KEYSTORE=$HOME/.keystore
keystore=$DEFAULT_KEYSTORE
pk8=""
cert=""
alias=""
passphrase=""
tmpdir=""
scriptname=`basename $0`
usage() {
cat &2
usage
exit 1
;;
esac
done
if [ -z "${pk8}" -o -z "${cert}" -o -z "${alias}" ]; then
echo "${scriptname}: Missing option, exiting..." 1>&2
usage
exit 1
fi
for f in "${pk8}" "${cert}"; do
if [ ! -f "$f" ]; then
echo "${scriptname}: Can't find file $f, exiting..." 1>&2
exit 1
fi
done
if [ ! -f "${keystore}" ]; then
storedir=`dirname "${keystore}"`
if [ ! -d "${storedir}" -o ! -w "${storedir}" ]; then
echo "${scriptname}: Can't access ${storedir}, exiting..." 1>&2
exit 1
fi
fi
# Create temp directory ofr key and pkcs12 bundle
tmpdir=`mktemp -q -d "/tmp/${scriptname}.XXXX"`
if [ $? -ne 0 ]; then
echo "${scriptname}: Can't create temp directory, exiting..." 1>&2
exit 1
fi
key="${tmpdir}/key"
p12="${tmpdir}/p12"
if [ -z "${passphrase}" ]; then
# Request a passphrase
read -p "Enter a passphrase: " -s passphrase
echo ""
fi
# Convert PK8 to PEM KEY
openssl pkcs8 -inform DER -nocrypt -in "${pk8}" -out "${key}"
# Bundle CERT and KEY
openssl pkcs12 -export -in "${cert}" -inkey "${key}" -out "${p12}" -password pass:"${passphrase}" -name "${alias}"
# Print cert
echo -n "Importing
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?