1.intent和PendingIntent的区别

intent表示执行某种意图
pendingIntent表示暂缓执行某种意图,直到遇到特殊条件才执行

2.发送通知:Notification

参考文章
Notification表示的事一种提示用户操作的组件(就是滑屏下拉的通知)

public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//用来发通知的类NotificationManager notificationManager= (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);//创建被发送的信息Notification notification=new Notification(R.drawable.dress23,"来自fengray的提示",System.currentTimeMillis());//立刻发送信息//创建pendingIntent对象PendingIntent contentIntent=PendingIntent.getActivity(this,0,getIntent(),PendingIntent.FLAG_UPDATE_CURRENT);//创建PendingItent对象//执行发送消息notification.setLatestEventInfo(this,"fengrayTech","fengray Tech.co",contentIntent);notificationManager.notify("mldn",R.drawable.dress24,notification);}
}

安卓6以后已经取消了setLatestEventInfo方法,全面是使用Notification.Builder方法
如:

public class MainActivity extends AppCompatActivity {@RequiresApi(api = Build.VERSION_CODES.O)@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);/*首先需要一个NotificationManager来对通知进行管理调用Context的getSystemService()方法获取到。getSystemService()方法接受的一个字符串参数用于确定系统的的哪一个服务。*/NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//创建pendingIntent对象PendingIntent pendingIntent=PendingIntent.getActivity(this,0,getIntent(),PendingIntent.FLAG_UPDATE_CURRENT);//创建PendingItent对象//通知渠道的IDString id = "channel_01";Notification notification = new NotificationCompat.Builder(MainActivity.this,id)//指定通知的标题内容.setContentTitle("fengray")//设置通知的内容.setContentText("this is message from frengray")//指定通知被创建的时间.setWhen(System.currentTimeMillis())//设置通知的小图标.setSmallIcon(R.drawable.dress23)//设置通知的大图标.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.dress24))//添加点击跳转通知跳转.setContentIntent(pendingIntent)//实现点击跳转后关闭通知.setAutoCancel(true).build();/*
调用NotificationManager的notify()方法将通知显示出来
传入的第一个参数是通知的id
传入的第二个参数是notification对象*/notificationManager.notify(1,notification);}
}

也有结合通道的方式创建通知:

public class MainActivity extends AppCompatActivity {@RequiresApi(api = Build.VERSION_CODES.O)@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);/*首先需要一个NotificationManager来对通知进行管理调用Context的getSystemService()方法获取到。getSystemService()方法接受的一个字符串参数用于确定系统的的哪一个服务。*/NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//创建pendingIntent对象PendingIntent pendingIntent=PendingIntent.getActivity(this,0,getIntent(),PendingIntent.FLAG_UPDATE_CURRENT);//创建PendingItent对象/*调用NotificationChannel创建通知渠道实例并为它设置属性*///通知渠道的IDString id = "channel_01";//用户可以看到的通知渠道的名字CharSequence name = "MyChannel";//用户可看到的通知描述String description ="here is my channel";//构建NotificationChannel实例NotificationChannel notificationChannel =new NotificationChannel(id,name,NotificationManager.IMPORTANCE_HIGH);//配置通知渠道的属性notificationChannel.setDescription(description);//设置通知出现时的闪光灯notificationChannel.enableLights(true);notificationChannel.setLightColor(Color.RED);//设置通知出现时的震动notificationChannel.enableVibration(true);notificationChannel.setVibrationPattern(new long[]{100,200,300,400,500,400,300,200,100});//在notificationManager中创建通知渠道notificationManager.createNotificationChannel(notificationChannel);Notification notification = new NotificationCompat.Builder(MainActivity.this,id)//指定通知的标题内容.setContentTitle("fengray")//设置通知的内容.setContentText("this is message from frengray")//指定通知被创建的时间.setWhen(System.currentTimeMillis())//设置通知的小图标.setSmallIcon(R.drawable.dress23)//设置通知的大图标.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.dress24))//添加点击跳转通知跳转.setContentIntent(pendingIntent)//实现点击跳转后关闭通知.setAutoCancel(true).build();/*
调用NotificationManager的notify()方法将通知显示出来
传入的第一个参数是通知的id
传入的第二个参数是notification对象*/notificationManager.notify(1,notification);}
}

结果:

3.发送短信(非主流方式)


利用类 SmsManager 发送信息, smsManager 为 SmsManager 一个默认的实例. SmsManager smsManager = SmsManager.getDefault();
  smsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)
具体参数为:
  destinationAddress: 收件人号码
  scAddress: 短信中心服务号码, 这里设置为null
  text: 发送内容
  sentIntent: 发送短信结果状态信号(是否成功发送),new 一个Intent , 操作系统接收到信号后将广播这个Intent.此过程为异步.
  deliveryIntent: 对方接收状态信号(是否已成功接收).

public void onClick(View v) {switch (v.getId()) {case R.id.btnSent:/*** 实现短信发送功能*/// 获取默认短信管理对象SmsManager smsManager = SmsManager.getDefault();//安卓6以后好像被弃用了,新版本不认getDefault()// 判断发送内容字数(一件信息最多70 字)if(SMSContent.length() <= 70) {smsManager.sendTextMessage(SMSTo, null, SMSContent, null, null);}else{// SmsManger 类中 divideMessage 会将信息按每70 字分割List<String> smsDivs = smsManager.divideMessage(SMSContent);for(String sms : smsDivs) {smsManager.sendTextMessage(SMSTo, null, sms, null, null);}}Toast.makeText(PhoneSMSActivity.this, "信息已发送", Toast.LENGTH_SHORT);break;}}

魔乐科技安卓开发教程----李兴华----06PendingIntent相关推荐

  1. 魔乐科技安卓开发教程----李兴华----08APPWidget

    1.认识Widget 通过桌面的一些软件窗口实现对程序得控制,需要使用的类 RemotView 2.创建一个Widget 1.创建一个类,继承自AppWidgetProvider public cla ...

  2. 魔乐科技安卓开发教程----李兴华----01文件存储

    1.查看模拟器data文件夹 到-sdk\platform-tools文件夹下Shift+鼠标右键打开命令窗口(win7),输入adb root回车然后输入adb remount回车, 出现remou ...

  3. 魔乐科技安卓开发教程----李兴华----13视频录制

    1.录制视频 1.添加各种权限及横屏属性 //传感器决定 参考: Activity的screenOrientation属性详解 <uses-permission android:name=&qu ...

  4. 魔乐科技安卓开发教程----李兴华----07BroadCast广播

    1.认识广播 1.建立广播接收器(类)MyBroadcastReceiver并继承BroadcastReceiver public class MyBroadcastReceiver extends ...

  5. 魔乐科技安卓开发教程----李兴华----05Service

    1.开启.终止服务 1.布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&quo ...

  6. 魔乐科技安卓开发教程----李兴华----10动画

    1.渐变动画TweenAnimation 2.创建透明度变化 1.主布局文件 <LinearLayout xmlns:android="http://schemas.android.c ...

  7. 魔乐科技安卓开发教程----李兴华----12音频录制

    1.MediaRecorder 2.MediaRecorder类的生命周期 2.一个录音的案例 点击下载本例代码 1.定义列表的布局 创建布局文件recode_files.xml <TableL ...

  8. 魔乐科技安卓开发教程----李兴华----19 传感器

    1.传感器常用方法及常量 android中支持的传感器 2.移动小球(方位传感器) 1.创建一个自定义的View类,Ballview public class BallView extends Vie ...

  9. 魔乐科技安卓开发教程----李兴华----03菜单

    1.ActivityGroup+GridView方式的基本菜单 1.创建用于显示icon的activity:MyActivity及其布局文件mylayout.xml mylayout <Line ...

最新文章

  1. QS世界大学最新排名公布:清华超过耶鲁,MIT仍居榜首,12所中国高校跻身百强...
  2. 什么原因导致芯片短路_常见的芯片故障现象
  3. oracle9i在window server 2003 sp2 企业版突破1.7G内存限制
  4. linux java远程调试_[转]JPDA:Java平台调试架构(常用的远程调试方法)
  5. SQL调用C# dll(第一中DLL,没使用强名称密匙,默认是 safe)
  6. Python url中提取域名(获取域名、获取顶级域名、tldextract)
  7. su生成面域插件_插件玩的溜,SU不用愁
  8. 埃氏筛 线性筛(欧拉筛) 算法解析
  9. 自定义广播增加权限控制
  10. 希沃集控系统流媒体服务器未开启,希沃集控,让教育信息化管理尽在“掌控”之中...
  11. 硬件-1-打印机爱普生L3153墨仓式一体机
  12. 爬取豆瓣网新书传递信息,关系型数据库的储存
  13. 一款快速搭建局域网http服务器的神器
  14. 分享typecho博客的Next主题包
  15. GCDLCM 【米勒_拉宾素数检验 (判断大素数)】
  16. Ansible-template模块使用(jinjia2模板)
  17. js 校验手机号码格式
  18. EXCEL快速提取中英文、数字的4个方法,总有一个适合你!
  19. chrome浏览器主页被劫持为hao123
  20. Unity实现BStar寻路

热门文章

  1. php生成网名,制作网名的软件
  2. 9.0以下免root框架,免 root 框架
  3. 一款超级简单的后台管理系统模板
  4. 使VLC媒体播放器看起来很棒的10大皮肤
  5. r语言变量长度不一致怎么办_基础方法 | 数据管理:Stata与R语言的应用
  6. android ui 框架
  7. 个人 IP 打造方法
  8. 描述生活日常的句子、语录、短句、说说、文案
  9. 关于SQLAlchemy的警告Warning: (1366, “Incorrect string value: ‘\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...‘ for colu
  10. Hibernate映射文件生成器by LDDXFS