Android AIDL

    xiaoxiao2025-01-31  4

    -----------------------AIDL--------------------------------- Server: 启动和关联一项服务: Intent intent = new Intent(MainActivity.this,MyService.class); bindService(intent, conn, Service.BIND_AUTO_CREATE); 关闭服务: unbindService(conn);             绑定到一个Service 操作示例: private ServiceConnection conn = new ServiceConnection() {                  @Override         public void onServiceDisconnected(ComponentName name) {             // TODO Auto-generated method stub         }                  @Override         public void onServiceConnected(ComponentName name, IBinder service) {             // TODO Auto-generated method stub             iPerson = IPerson.Stub.asInterface(service);             if(iPerson!=null){                 try {                     iPerson.setValue("AIDL TEST");                     Toast.makeText(MainActivity.this, "Set: AIDL TEST", Toast.LENGTH_LONG).show();                 } catch (RemoteException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();                     Toast.makeText(MainActivity.this, "Error: AIDL TEST", Toast.LENGTH_LONG).show();                 }             }         }     }; client: 连接到Service: // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction("com.example.server.MyService"); bindService(intent, conn, Service.BIND_AUTO_CREATE); 客服到服务操作示例:     @Override         public synchronized void onServiceConnected(ComponentName name, IBinder service) {             // TODO Auto-generated method stub             person = IPerson.Stub.asInterface(service);             if(person != null){                 try {                     String name1 = person.getValue();                     Toast.makeText(MainActivity.this, "getValue "+name1, Toast.LENGTH_LONG).show();                 } catch (RemoteException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();                     Toast.makeText(MainActivity.this, "catch: getValue ", Toast.LENGTH_LONG).show();                 }             }         }     };      -------------------------API介绍-------------------------------------- 一: 下面列出几种Intent的用法 显示网页:   代码如下: Uri uri = Uri.parse("http://www.google.com"); Intent it  = new Intent(Intent.ACTION_VIEW,uri); startActivity(it); 显示地图:   代码如下: Uri uri = Uri.parse("geo:38.899533,-77.036476"); Intent it = new Intent(Intent.Action_VIEW,uri); startActivity(it); 路径规划:   代码如下: Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en"); Intent it = new Intent(Intent.ACTION_VIEW,URI); startActivity(it); 拨打电话: 调用拨号程序   代码如下: Uri uri = Uri.parse("tel:xxxxxx"); Intent it = new Intent(Intent.ACTION_DIAL, uri); startActivity(it); Uri uri = Uri.parse("tel.xxxxxx"); Intent it =new Intent(Intent.ACTION_CALL,uri); 要使用这个必须在配置文件中加入<uses-permission id="android.permission.CALL_PHONE" /> 发送SMS/MMS 调用发送短信的程序   代码如下: Intent it = new Intent(Intent.ACTION_VIEW);   it.putExtra("sms_body", "The SMS text");   it.setType("vnd.android-dir/mms-sms");   startActivity(it); 发送短信   代码如下: Uri uri = Uri.parse("smsto:0800000123");   Intent it = new Intent(Intent.ACTION_SENDTO, uri);   it.putExtra("sms_body", "The SMS text");   startActivity(it); 发送彩信   代码如下: Uri uri = Uri.parse("content://media/external/images/media/23");   Intent it = new Intent(Intent.ACTION_SEND);   it.putExtra("sms_body", "some text");   it.putExtra(Intent.EXTRA_STREAM, uri);   it.setType("image/png");   startActivity(it); 发送Email   代码如下: Uri uri = Uri.parse("mailto:xxx@abc.com"); Intent it = new Intent(Intent.ACTION_SENDTO, uri); startActivity(it); Intent it = new Intent(Intent.ACTION_SEND);   it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");   it.putExtra(Intent.EXTRA_TEXT, "The email body text");   it.setType("text/plain");   startActivity(Intent.createChooser(it, "Choose Email Client")); Intent it=new Intent(Intent.ACTION_SEND);     String[] tos={"me@abc.com"};     String[] ccs={"you@abc.com"};     it.putExtra(Intent.EXTRA_EMAIL, tos);     it.putExtra(Intent.EXTRA_CC, ccs);     it.putExtra(Intent.EXTRA_TEXT, "The email body text");     it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");     it.setType("message/rfc822");     startActivity(Intent.createChooser(it, "Choose Email Client")); 添加附件   代码如下: Intent it = new Intent(Intent.ACTION_SEND);   it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");   it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");   sendIntent.setType("audio/mp3");   startActivity(Intent.createChooser(it, "Choose Email Client")); 播放多媒体   代码如下:   Intent it = new Intent(Intent.ACTION_VIEW); Uri uri = Uri.parse("file:///sdcard/song.mp3"); it.setDataAndType(uri, "audio/mp3"); startActivity(it); Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");   Intent it = new Intent(Intent.ACTION_VIEW, uri);   startActivity(it); Uninstall 程序   代码如下: Uri uri = Uri.fromParts("package", strPackageName, null);   Intent it = new Intent(Intent.ACTION_DELETE, uri);   startActivity(it); 二:bindService()使用方法: http://www.cnblogs.com/yejiurui/archive/2013/11/18/3429451.html bindService是绑定Service服务,执行service服务中的逻辑流程。 service通过Context.startService()方法开始,通过Context.stopService()方法停止;也可以通过Service.stopSelf()方法或者Service.stopSelfResult()方法来停止自己。只要调用一次stopService()方法便可以停止服务,无论之前它被调用了多少次的启动服务方法。 客户端建立一个与Service的连接,并使用此连接与Service进行通话,通过Context.bindService()方法来绑定服务,Context.unbindService()方法来关闭服务。多个客户端可以绑定同一个服务,如果Service还未被启动,bindService()方法可以启动服务。 上面startService()和bindService()两种模式是完全独立的。你可以绑定一个已经通过startService()方法启动的服务。例如:一个后台播放音乐服务可以通过startService(intend)对象来播放音乐。可能用户在播放过程中要执行一些操作比如获取歌曲的一些信息,此时activity可以通过调用bindServices()方法与Service建立连接。这种情况下,stopServices()方法实际上不会停止服务,直到最后一次绑定关闭。     如果没有程序停止它或者它自己停止,service将一直运行。在这种模式下,service开始于调用Context.startService() ,停止于Context.stopService(). service可以通过调用Android Service 生命周期() 或 Service.stopSelfResult()停止自己。不管调用多少次startService() ,只需要调用一次 stopService() 就可以停止service。     可以通过接口被外部程序调用。外部程序建立到service的连接,通过连接来操作service。建立连接调开始于Context.bindService(), 结束于Context.unbindService(). 多个客户端可以绑定到同一个service,如果service没有启动, bindService() 可以选择启动它。     这2种模式不是完全分离的。你可以可以绑定到一个通过startService()启动的服务。如一个intent想要播放音乐,通过startService() 方法启动后台播放音乐的service。然后,也许用户想要操作播放器或者获取当前正在播放的乐曲的信息,一个activity就会通过bindService()建立一个到此service的连接. 这种情况下 stopService() 在全部的连接关闭后才会真正停止service。 三:Android ServiceConnection 分析: http://blog.csdn.net/lansefeiyang08/article/details/24691363 应用组件(客户端)可以调用bindService()绑定到一个service.Android系统之后调用service的onBind()方法,它返回一个用来与service交互的IBinder.   绑定是异步的.bindService()会立即返回,它不会返回IBinder给客户端.要接收IBinder,客户端必须创建一个ServiceConnection的实例并传给bindService().ServiceConnection包含一个回调方法,系统调用这个方法来传递要返回的IBinder. 所以,从你的客户端绑定到一个service,你必须:     1实现ServiceConnection.     你的实现必须重写两个回调方法:         onServiceConnected()         系统调用这个来传送在service的onBind()中返回的IBinder.        OnServiceDisconnected()Android系统在同service的连接意外丢失时调用这个.比如当service崩溃了或被强杀了.当客户端解除绑定时,这个方法不会被调用.     2调用bindService(),传给它ServiceConnection的实现.     3当系统调用你的onServiceConnected()方法时,你就可以使用接口定义的方法们开始调用service了.     4要与service断开连接,调用unbindService().     
    转载请注明原文地址: https://ju.6miu.com/read-1295954.html
    最新回复(0)