黑马Android76期学习笔记01基础--day07--广播,有、无序广播、特殊广播接受者、样式和主题,this与context的区别、普通对话框,进度条对话框、帧动画
1.广播Broadcastreceiver
1、定义一个广播接收者(定义一个类并继承BroadcastReceiver)
public class OutGoingCallReveiver extends BroadcastReceiver
2、在清单文件里面进行配置
<receiver android:name=".OutGoingCallReveiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
案例
1、清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fengray.ex059">
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".OutGoingCallReveiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
</application>
</manifest>
2、自定义一个广播处理类,继承自BroadcastReceiver
package com.fengray.ex059;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
public class OutGoingCallReveiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获得sharePerference中ip号码,在config.xml中存储,因为广播没有上下文,所以这边通过参数传递过来一个上下文
SharedPreferences sharedPreferences= context.getSharedPreferences(“config”,0);
String ipnumber=sharedPreferences.getString(“ipnumber”,"");
//获取当前拨打的电话号码
String currentNumber=getResultData();
//判断当前的号码是否是长途
if (currentNumber.startsWith(“0”)){
//在当前的号码前加上17951
setResultData(ipnumber+currentNumber);
}
}
}
3、MainActivity类
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText edphoneNumber;
private Button btnsave;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edphoneNumber=findViewById(R.id.edphoneNumber);
}
//
public void btnClick(View view){
//获取号码
String ipNumber=edphoneNumber.getText().toString().trim();
//把当前ipNumber存起来,存在sp就行(sharePreference),sp实际是以config.xml的文件形式存储
SharedPreferences sharedPreferences = getSharedPreferences("config",0);
//获取sharepreferencs编辑器
sharedPreferences.edit().putString("ipnumber",ipNumber).commit();
//保存成功
Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();
}
}
2.SD卡状态监听
- 1、定义广播接受者
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class sdCardStateReceiver extends BroadcastReceiver {
//当sd卡状态改变的时候
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
if ("android.intent.action.MEDIA_MOUNTED".equals(action)){
Log.d("TAG", "sd card is mounted ");
}else if ("android.intent.action.MEDIA_UNMOUNTED".equals(action)){
Log.d("TAG", "sd card is UNmounted ");
}
}
}
- 2、在清单文件里面配置
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fengray.ex060">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".sdCardStateReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<data android:scheme="file"/>
</intent-filter>
</receiver>
</application>
</manifest>
3.短信状态监听
1、清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fengray.ex060">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".sdCardStateReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<data android:scheme="file"/>
</intent-filter>
</receiver>
<receiver android:name=".SmsListener">
<intent-filter>
<action android:name="android.provier.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
2、自定义SmsListenerReceiver继承自BroadcastReceiver
public class SmsListenerReceiver extends BroadcastReceiver {
//当sd卡状态改变的时候
@Override
public void onReceive(Context context, Intent intent) {
//获得key值为pdus的所有短信
Object[] messages =(Object[])intent.getExtras().get(“pdus”);
for (Object message:messages) {
//获取Message的实例
SmsMessage smsMessage=SmsMessage.createFromPdu((byte[]) message);
//获取发送短信的内容
String messageBody=smsMessage.getMessageBody();
String address= smsMessage.getOriginatingAddress();
Log.d("TAG", "body: "+messageBody+"from"+address);
}
}
}
4.卸载安装监听
1、清单文件
<receiver android:name=".AppStateReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
2、AppStateReceiver 类文件
public class AppStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获取当前广播的时间类型
String action=intent.getAction();
if (“android.intent.action.PACKAGE_INSTALL”.equals(action)){
Log.d(“TAG”, “应用安装了1111”);
}else if (“android.intent.action.PACKAGE_ADDED”.equals(action)){
Log.d(“TAG”, “应用安装了2222”);
}else if (“android.intent.action.PACKAGE_REMOVED”.equals(action)){
Log.d(“TAG”, “应用卸载了333”+intent.getData());
}
}
}
5.重启手机是启动指定页面
1、清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fengray.ex060">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2、BootReceiver 类
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//在这个方法里面开启activity
Intent newIntent=new Intent(context,MainActivity.class);
//不能再广播接受者当中直接开启activity,需要添加一个标记,添加一个任务栈的标记
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//在广播中开启activity
context.startActivity(newIntent);
}
}
6.无序广播
本例并没有实现,应该是高版本对广播权限有限制
1、清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fengray.ex060">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.fengray.ex060.CustromReceiver">
<intent-filter>
<action android:name="com.fengray.CunstomerAction"/>
</intent-filter>
</receiver>
</application>
</manifest>
**2、自定义广播类文件CustromReceiver **
package com.fengray.ex060;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class CustromReceiver extends BroadcastReceiver {
//当接受到自定义广播
@Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG", "onReceive: -------");
String content=intent.getStringExtra("name");
Log.d("TAG", "onReceive: "+content);
Toast.makeText(context, content, Toast.LENGTH_SHORT).show();
}
}
**3、MainActivity **
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//点击按钮发送一条无序广播
public void sendBroadcast(View view){
Intent intent=new Intent();
intent.setAction("com.fengray.CunstomerAction");
intent.putExtra("name","新闻联播每天晚上7点准时开播");
sendBroadcast(intent);
}
}
7.有序广播
有序广播
- 有序广播可以被终止
- 有序广播的数据可以被修改
无序广播
- 无序广播不可被终止
- 数据不可以被修改
需要在两个APP之间进行
ex061项目–广播发送者
项目结构
1、MainActivity
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//点击按钮发送有序广播
public void sendBroadcast(View view){
Intent intent=new Intent();
//intent.putExtra();
intent.setAction("com.fengray.sender");
//发送有序广播
//receiverPermission:接受的权限
//resualtReceiver:最终的receiver
//scheduler:是一个handler
//initialCode:初始码
//initialData:初始化数据
sendOrderedBroadcast(intent,null,new FinalReceiver(),null,1,"我是广播的内容", null );
}
}
2、FinalReceiver
public class FinalReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获取广播所携带的数据
String content=getResultData();
//将广播数据显示出来
Toast.makeText(context, "这是最终的广播接受者"+content, Toast.LENGTH_SHORT).show();
}
}
ex062项目–广播接受者
目录结构分了四级有序接受
1、ProvinceReceiver
public class ProvinceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获取广播所携带的数据
String content=getResultData();
//终止广播,后续级别都无法再接收到广播
//abortBroadcast();
//将广播数据显示出来
Toast.makeText(context, "省级"+content, Toast.LENGTH_SHORT).show();
//修改广播数据
setResultData("被省一级修改后的数据");
}
}
**2、CityReceiver **
public class CityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获取广播所携带的数据
String content=getResultData();
//将广播数据显示出来
Toast.makeText(context, "市级"+content, Toast.LENGTH_SHORT).show();
//修改广播数据
setResultData("被市一级修改后的数据");
}
}
**3、XianReceiver **
public class XianReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获取广播所携带的数据
String content=getResultData();
//将广播数据显示出来
Toast.makeText(context, "县级"+content, Toast.LENGTH_SHORT).show();
//修改广播数据
setResultData("被县一级修改后的数据");
}
}
**4、PersonReceiver **
public class PersonReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获取广播所携带的数据
String content=getResultData();
//将广播数据显示出来
Toast.makeText(context, "个人"+content, Toast.LENGTH_SHORT).show();
}
}
5、清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fengray.ex062receiver">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--配置省级receiver,优先级最高,priority值越大,优先级越高-->
<receiver android:name=".ProvinceReceiver">
<intent-filter android:priority="1000">
<action android:name="com.fengray.sender"/>
</intent-filter>
</receiver>
<!--配置市级receiver,优先级最高,priority值越大,优先级越高-->
<receiver android:name=".CityReceiver">
<intent-filter android:priority="800">
<action android:name="com.fengray.sender"/>
</intent-filter>
</receiver>
<!--配置县级receiver,优先级最高,priority值越大,优先级越高-->
<receiver android:name=".XianReceiver">
<intent-filter android:priority="400">
<action android:name="com.fengray.sender"/>
</intent-filter>
</receiver>
<!--配置个人receiver,优先级最高,priority值越大,优先级越高-->
<receiver android:name=".PersonReceiver">
<intent-filter android:priority="200">
<action android:name="com.fengray.sender"/>
</intent-filter>
</receiver>
</application>
</manifest>
8.特殊广播接受者
操作特别频繁的广播事件,如屏幕解锁,在清单文件中注册是无效的
注册广播接收者的2种方式
1、动态注册:通过代码方式注册
2、在清单文件通过receiver tag节点静态发布
1、ScreenReceiver 类
public class ScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获取当前广播事件的类型
String action=intent.getAction();
if ("android.intent.action.SCREEN_ON".equals(action)){
Log.d("TAG", "onReceive: 屏幕解锁了");
}else if ("android.intent.action.SCREEN_OFF".equals(action)){
Log.d("TAG", "onReceive: 屏幕锁屏了");
}
}
}
2、MainActivity
import androidx.appcompat.app.AppCompatActivity;
import android.content.IntentFilter;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private ScreenReceiver screenReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//动态注册广播接收者
//创建接收者
screenReceiver=new ScreenReceiver();
//创建intenFilter对象
IntentFilter intentFilter=new IntentFilter();
//添加要注册的action
intentFilter.addAction("android.intent.action.SCREEN_OFF");
intentFilter.addAction("android.intent.action.SCREEN_ON");
//动态注册广播接收者
registerReceiver(screenReceiver,intentFilter);
}
@Override
protected void onDestroy() {
//当activity销毁的时候要取消注册广播接受者
unregisterReceiver(screenReceiver);
}
}
9.国际化
在res目录下创建不同国家语言环境集目录,目录是固定写法values-en zh…
10.两种context
this 是getApplicationContext的子类,在对话框中只能使用this代表context,因为他带有个tag,而getApplicationContext没有。
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnClick(View view){
//通过AlertDialog.builder构造器来创建
AlertDialog.Builder builder=new AlertDialog.Builder(this);//这里不能是用getApplicationContext()
builder.setTitle("警告");
builder.setMessage("世界上最遥远的距离是没有网络");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("TAG", "onClick: 您点击了确定按钮");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("TAG", "onClick: 您点击了取消按钮");
}
});
builder.show();
}
}
11.单选对话框
//单选对话框
public void btnClick(View view){
//通过AlertDialog.builder构造器来创建
AlertDialog.Builder builder=new AlertDialog.Builder(this);//这里不能是用getApplicationContext()
builder.setTitle("请选择你喜欢的课程");
//单选项的字符串数组
final String items[]={"android","IOS","switf","kotlin","c"};
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//把选中的条目选出来
String item=items[which];
Toast.makeText(getApplicationContext(), item, Toast.LENGTH_SHORT).show();
//关闭对话框
dialog.dismiss();
}
});
builder.show();
}
12.多选对话框
//多选对话框
public void btnClick(View view){
//通过AlertDialog.builder构造器来创建
AlertDialog.Builder builder=new AlertDialog.Builder(this);//这里不能是用getApplicationContext()
builder.setTitle("请选择你喜欢的课程");
//单选项的字符串数组
final String items[]={"android","IOS","switf","kotlin","c"};
final boolean[] checkedItems={true,false,false,true,false};//是否被选中
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//把选中的条目的数据取出来
StringBuffer stringBuffer=new StringBuffer();
for (int i=0; i<items.length;i++){
//判断一下选中的条目
if(checkedItems[i]){
String itemcontent=items[i];
stringBuffer.append(itemcontent+" ");
}
}
Toast.makeText(getApplicationContext(), stringBuffer.toString(), Toast.LENGTH_SHORT).show();
//关闭对话框
dialog.dismiss();
}
});
builder.show();
}
}
13.进度条对话框
//进度条对话框
public void btnClick(View view){
//通过AlertDialog.builder构造器来创建
final ProgressDialog dialog=new ProgressDialog(this);
dialog.setTitle("正在玩命加载中……");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.show();
new Thread(){
@Override
public void run() {
dialog.setMax(100);
for (int i=0;i<100;i++){
dialog.setProgress(i);
//睡眠一定的时间
SystemClock.sleep(50);
}
//关闭对话框
dialog.dismiss();
}
}.start();
}
}
14.帧动画
引入动画资源,在drawable当中引入动画图片
引入连续图片
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/banana01" android:duration="200"/>
<item android:drawable="@drawable/banana02" android:duration="200"/>
<item android:drawable="@drawable/banana03" android:duration="200"/>
<item android:drawable="@drawable/banana04" android:duration="200"/>
<item android:drawable="@drawable/banana05" android:duration="200"/>
</animation-list>
MainActivity
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mytv;
private ImageView myimage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myimage=findViewById(R.id.myimg);
//设置imageview的背景资源
myimage.setBackgroundResource(R.drawable.myanim);
//获取animationDrawable类型
AnimationDrawable imageAnimation=(AnimationDrawable)myimage.getBackground();
//开启动画
imageAnimation.start();
}
}
本文地址:https://blog.csdn.net/weixin_43745804/article/details/108136944