广播接收者

    首先,我们来认识什么是BroadReceiver?翻译成中文的意思:广播接收者。对于我来说,每学新知识之前先看看官网上对此怎么介绍的

来让我们看看官网介绍吧。

1>对该类的介绍(Class Overview)

广播接收者可以接收通过sendBroadcast(),然而你也能通过Content.registReceiver()这个方法来动态的注册这个类的实例或者通过你通过在AndroidManifest.xml这个文件中声明<receiver>这个标记(tag)静态的得到该类的对象。

-------------------------------------------------------------------------------------------------------------

IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
IncomingSMSReceiver receiver = new IncomingSMSReceiver();
registerReceiver(receiver, filter);
          
<receiver android:name=".IncomingSMSReceiver">
     <intent-filter>
          <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
     </intent-filter>
</receiver>
-------------------------------------------------------------------------------------------------------------

Android中广播事件机制 Broadcast Receiver

broadcast 广播  Receiver 接收

案例:Activity发出广播 , 传递消息

      Receiver接受者,接收消息

Broadcast 广播 使用Intent发送和传递消息

----------------------------------------------------------

Intent intent = new Intent();

intent.setAction("com.android.app.receiver.MY_RECEIVER");

intent.putExtra("msg","xxxxxxxxxxxx");

sendBroadcast(Intent intent);

Receiver接收器

class Myreceiver extends BroadcastReceiver{

   pubilc void onReceive(Context context,Intent intent){

      获取广播发送的信息,并进行处理

      String msg = intent.getStringExtra("msg");

      显示......

   }

}

----------------------------------------------------------

AndroidManifest.xml

<receiver android:name="com.android.app.receiver.Myreceiver">

  <intent-filter>

      <action android:name="com.android.app.receiver.MY_RECEIVER"/>

  </intent-filter>

</receiver>

----------------------------------------------------------

2>Android中的Notification通知

涉及一些基本的类

Notification 通知类

NotificationManager 通知管理类  notify

1、创建一个Notification的对象

2、设置Notification中的基本元素 (图标  尖端标题  发布事件)

3、创建NotificationManager的对象

   (NotificationManager)getSystemService(NOTIFICATION_SERVICE)

4、创建Intent对象

5、创建PendingIntent重新包装Intent对象

6、设置具体的通知内容(标题,内容)

7NotificationManager的对象的notify方法发送Notification的对象

BroadcastReceiver中启动另外一个Activity

----------------------------------------------------------

public void onReceive(Context context, Intent intent) {

Intent i = new Intent();

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

// 设置定向的Activity

i.setClass(context, DisplayActivity.class);

// 页面跳转

context.startActivity(i);

}

----------------------------------------------------------

3>AlarmManager的用法

// 声明一个AlarmManager管理器类
private AlarmManager alarmManager;
// 声明一个PendingIntetn对象
private PendingIntent pendingIntent;

 // 封装一个方法完成闹钟的开启动作
    private void startAlarm(){
    // 实例化AlarmManager对象
    alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    // 创建一个Intent对象
    Intent intent = new Intent();
    // 设置intent对象的Action属性
    intent.setAction("com.android.app.receiver.MY_RECEIVER");
    // 设置intent对象的参数
    intent.putExtra("msg", "吃饭了");
    // 实例化PendingIntent对象
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
    // 间隔性启动闹钟
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 8*1000, pendingIntent);
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值