ADL和通知

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对象转化成远程服务中的接口类型,最终通过这个对象实现调用远程方法
创建一个类继承Service
···
public class RemoteService extends Service{
@OverridepublicIBinderonBind(Intent intent){returnnewMyBinder(); }publicvoidremotoMethod(){ Log.e("TAG","远程方法被调用"); }publicclassMyBinderextendsStub{//自己包名下的StubpublicvoidcallRemotoMethod(){ remotoMethod(); } }}
···
定义一个接口

里面实现方法
如:
void remotoMethod();
注意:不能用public 修饰
在创建一个项目;
xml
···
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="调用远程方法"
android:onClick="callRemote"
/>
MainAction中
···
public class MainActivity extends Activity {
private MyConntention conn;
private IService iservice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent service = new Intent();
conn=new MyConntention();
//隐式意图开启其他应用
service.setAction("com.krr.remotoservice");
bindService(service, conn, BIND_AUTO_CREATE);
}publicvoidcallRemote(View v){try{ iservice.callRemotoMethod();//远程调用}catch(RemoteException e) { e.printStackTrace(); } }publicclassMyConntentionimplementsServiceConnection{@OverridepublicvoidonServiceConnected(ComponentName name, IBinder service){ iservice =Stub.asInterface(service); }@OverridepublicvoidonServiceDisconnected(ComponentName name){ } }}
通知:
在xml中*************
定义一个按钮
···
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击显示通知"
android:onClick="click" />
···
在MainActivity中实现这个方法-
···
public class MainActivity extends Activity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); }
publicvoidclick(View v){
//1.获取手机系统的 通知的管理器NotificationManager nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);//获取系统服务
//2.实例化Notification 表示通知的具体内容Notification notification=newNotification( R.drawable.ic_launcher,"我是一个通知",System.currentTimeMillis());//点击之后 自动取消notification.flags=Notification.FLAG_AUTO_CANCEL; Intent intent =newIntent();
//启动打电话intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:110"));
 PendingIntent contentIntent =PendingIntent.getActivity(this,0, intent,0); 
notification.setLatestEventInfo(this,"我是通知的标题","我是通知内容", contentIntent);
//2.实例化Notification 表示通知的具体内容// Notification.Builder builder=new Notification.Builder(this);
// builder .setContentTitle("我是通知的标题")
// .setContentText("我是通知内容")
// .setSmallIcon(R.drawable.f097)//小图标
// .setLargeIcon(BitmapFactory.decodeResource(
// getResources(),R.drawable.ic_launcher));
// Notification notification= builder.build();
//
nm.notify(0,notification);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值