• Android组件通信——PendingIntent(二十八)


    1. PendingIntent

    1.1 知识点

    (1)了解PendingIntent与Intent的区别;

    (2)可以完成Notification功能的开发;

    (3)可以使用PendingIntent进行短信的发送;

    1.2 具体内容

    Intent的功能主要是完成:一个Activity跳转到另外一个Activity或者是Service,表示的是一种操作意图;表示立即执行

    PendingIntent:表示暂不执行的操作意图,是一种在某一个事件之后才进行操作的Intent对象。表示暂缓执行,遇到特殊的条件才执行。

    范例:发送通知:Notifaction,表示的是提示用户操作组件。

    1. package com.example.notificationproject;
    2. import android.app.Activity;
    3. import android.app.Notification;
    4. import android.app.NotificationManager;
    5. import android.app.PendingIntent;
    6. import android.content.Context;
    7. import android.os.Bundle;
    8. public class NotificationActivity extends Activity {
    9. @Override
    10. protected void onCreate(Bundle savedInstanceState) {
    11. super.onCreate(savedInstanceState);
    12. super.setContentView(R.layout.activity_notification);
    13. NotificationManager notificationManager = (NotificationManager) super
    14. .getSystemService(Context.NOTIFICATION_SERVICE);// 取得系统服务
    15. Notification notification = new Notification(R.drawable.logo,// 显示图标
    16. "来自毛栗子消息",// 信息提示
    17. System.currentTimeMillis());// 显示时间
    18. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
    19. this.getIntent(), PendingIntent.FLAG_UPDATE_CURRENT);// 取得PendingIntent对象
    20. notification.setLatestEventInfo(this, "maolizi", "毛栗子(www.csdn.com)", pendingIntent);//设置信息
    21. notificationManager.notify("maolizi",R.drawable.logo,notification);//发送消息
    22. }
    23. }

     本程序就是取得通知服务,而后将通知发送出去,可以发现,当用户打开这个通知之后,就可以立刻回到指定的Intent,就是说是用户触发的时候才会进行跳转。

    这次只是针对这个服务进行一些基本的讲解,以后大家去学习手机服务的时候,会有更多更好玩的东西。

    1. public void sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)

    ·String destinationAddress:表示收件人的地址

    ·String scAddress:设置短信的号码。如果为null的话,表示是手机中心号码

    ·String text:短信内容

    ·PendingInteng sentIntent:当信息发出之后,会通过PendingIntent来接受成功或者失败的信息报告,如果此参数为空,则会检查所有未知的应用程序,会消耗很长的事件

    ·PendingIntent deliveryIntent:当信息发送到收件处时,该PendingIntent触发。 

    1. package com.example.notificationproject;
    2. import java.util.Iterator;
    3. import java.util.List;
    4. import android.app.Activity;
    5. import android.app.PendingIntent;
    6. import android.os.Bundle;
    7. import android.telephony.gsm.SmsManager;
    8. import android.widget.Toast;
    9. public class NotificationActivity extends Activity {
    10. @Override
    11. protected void onCreate(Bundle savedInstanceState) {
    12. super.onCreate(savedInstanceState);
    13. super.setContentView(R.layout.activity_notification);
    14. String content = "毛栗子CSDN博客于2017年开始,现在码龄6年,已写博客90多篇,上传资源4个,阅读量快突破四万。主要撰写关于Java、Android、Linux方面的内容。";
    15. SmsManager smsManager = SmsManager.getDefault();//短信管理类
    16. PendingIntent sentIntent = PendingIntent.getActivity(this, 0, super.getIntent(), PendingIntent.FLAG_UPDATE_CURRENT);
    17. if(content.length()>70){
    18. List res = smsManager.divideMessage(content);//拆分短信
    19. Iterator it = res.iterator();
    20. while(it.hasNext()){
    21. String msg = it.next();
    22. smsManager.sendTextMessage("15555215554", null, msg, sentIntent, null);//发送短信
    23. }
    24. }else{
    25. smsManager.sendTextMessage("15555215554", null, content, sentIntent, null);//发送短信
    26. }
    27. Toast.makeText(this, "短信发送完成", Toast.LENGTH_LONG).show();
    28. }
    29. }

    配置权限:

    "android.permission.SEND_SMS"/>

     1.3 小结

    (1)PendingIntent与Intent的最大区别在于一个将要执行,一个立即执行;

    (2)在Android中的系统服务有许多的操作都是需要在某一特定环境下才会执行的Intent,这种操作就使用PendingIntent完成;

  • 相关阅读:
    在win10中下载桌面版的docker并在docker中搭建运行基于linux的容器
    有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
    动态规划之简单多状态
    在Azure上使用Portal查出造成SQL注入攻击的语句
    matlab simulink 模糊变论域控制电梯四分之一模型
    LiteFlow v2.9.4发布!一款能让你系统支持热更新,编排,脚本编写逻辑的国产规则引擎框架
    Java代码审计15之Apache log4j2漏洞
    【节能学院】安科瑞餐饮油烟监测云平台助力大气污染攻坚战
    MySQL---JDBC编程
    AI二次开发C#图案颜色
  • 原文地址:https://blog.csdn.net/weixin_41830242/article/details/133840913