通知

 

什么是AIDL

1、AIDL(Android Interface Definition Language安卓接口定义语言):让其它应用可以调用当前应用Service中的方法

2、Android系统中的进程之间不能共享内存,因此,需要提供一些机制在不同进程之间进行数据通信

3、RPC(Remote Procedure Call远程过程调用):AIDL解决的就是RPC的问题

4、IPC(Inter Process Communication)进程间通信

5、每一个Android应用都运行在独立的进程中,所以应用之间的通信就是进程间通信

6、Activity通过Intent可调起其它应用,也可传递数据

7、BroadcastReceiver通过onReceive方法,可以处理其它应用发来的广播,都是通过Intent携带数据

 

AIDL的实现过程

1、提供远程服务方法的应用

1.创建一个Service,重写onBind方法,在onBind中返回一个Binder对象,需要远程调用的方法放到这个Binder对象中

2.在清单文件中声明对应的Service,需要添加一个intent-filter,可以通过隐式意图调用Service

3.创建一个接口,需要暴露给其它应用调用的方法都声明在这个接口中

4.把接口文件的扩展名修改为.aidl,需要注意的是,.aidl文件不支持public关键字,如果aidl创建得没有问题,就会在gen目录下生成一个IService.java

5.修改Service的代码,让MyBinder继承Stub

2、远程调用服务的应用

1.通过隐式意图以及bindService的方式开启远程服务

2.创建ServiceConnection的实现类

3.在当前应用中创建一个目录,目录结构要跟提供远程服务的应用的aidl文件所在目录结构保持一致,把aidl文件拷贝过来,如果没有问题,会在gen目录下生成一个IService.java文件,包名跟aidl文件的包名一致

4.在onServiceConnected方法中,通过Stub.asInterface(service)把当前的IBinder对象转化成远程服务中的接口类型,最终通过这个对象实现调用远程方法

 

package com.example.text27_notification;

import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 
 }
 
 public void click(View v){
  //1.获取到手机系统里面的通知管理器
  NotificationManager nm =
    (NotificationManager) getSystemService
    (NOTIFICATION_SERVICE);
//实例化NotificationManager, 表示通知具体内容
 Notification notification = new
   Notification(R.drawable.ic_launcher,"我是一个Notification",
    
     System.currentTimeMillis());
 //点击之后自动取消
 notification.flags=Notification.FLAG_AUTO_CANCEL;
 Intent intent = new Intent();
 intent.setAction(Intent.ACTION_CALL);
 intent.setData(Uri.parse("tel:110"));
           PendingIntent contentIntent =
             PendingIntent.getActivity
             (this, 0,
               intent, 0);
 notification.setLatestEventInfo(this,
   "我是Notification 的标题", "我是Notification的内容", contentIntent);
//  Notification.Builder builder = new Builder(this);
//  builder.setContentTitle("我是Notification 的标题" )
//        .setContentText("我是Notification的内容")
//        .setSmallIcon(R.drawable.ic_launcher)
//        .setLargeIcon(BitmapFactory.
//          decodeResource(getResources(), R.drawable.ic_launcher));
//  Notification notification =builder .build();
//
//  nm.notify(0, notification);
 }
}

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击显示通知"
        android:onClick="click"/>
        
</RelativeLayout>

 

转载于:https://www.cnblogs.com/feng8026/p/9088428.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值