Android 调用Remote Service方法
来源:互联网 发布:剑网三叽萝捏脸数据 编辑:程序博客网 时间:2024/06/08 06:04
《Android开发入门与实践》上的ex_TestServiceHolder例子在AndroidManifest.xml中
<service android:enabled="true" android:name=".TestService" android:process=":remote"/>
这是一个远程方法,是无法直接从Activity调用Service中的方法的。
根据网上的资料写了一个AIDL文件,实现了远程调用。在这里记录备忘。
package com.iceskysl.TestServiceHolder; import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class TestServiceHolder extends Activity { private boolean _isBound; private ITestService _boundService = null; private static final String TAG = "TestServiceHolder"; private IBinder _service; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setTitle("Service Test"); initButtons(); } private ServiceConnection _connection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { _boundService = ITestService.Stub.asInterface(service); Toast.makeText(TestServiceHolder.this, "Service connected", Toast.LENGTH_SHORT).show(); Log.e(TAG, "=====>onServiceConnected()"); } public void onServiceDisconnected(ComponentName className) { // unexpectedly disconnected,we should never see this happen. _boundService = null; Toast.makeText(TestServiceHolder.this, "Service connected", Toast.LENGTH_SHORT).show(); Log.e(TAG, "<=====onServiceDisconnected()"); } }; private void initButtons() { Button buttonStart = (Button) findViewById(R.id.start_service); buttonStart.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { startService(); } }); Button buttonStop = (Button) findViewById(R.id.stop_service); buttonStop.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { stopService(); } }); Button buttonBind = (Button) findViewById(R.id.bind_service); buttonBind.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { bindService(); } }); Button buttonUnbind = (Button) findViewById(R.id.unbind_service); buttonUnbind.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { unbindService(); } }); } private void startService() { Intent i = new Intent(this, TestService.class); this.startService(i); } private void stopService() { Intent i = new Intent(this, TestService.class); this.stopService(i); } private void bindService() { Intent i = new Intent(this, TestService.class); bindService(i, _connection, Context.BIND_AUTO_CREATE); _isBound = true; } private void unbindService() { if (_boundService != null) { try { Log.e(TAG,"_boundService.getCount() = "+ _boundService.getCount()); } catch (RemoteException e) { e.printStackTrace(); } } if (_isBound) { unbindService(_connection); _isBound = false; } }}
===============================================================
package com.iceskysl.TestServiceHolder; import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;public class TestService extends Service { private static final String TAG = "TestService"; private NotificationManager _nm; private boolean threadEnable = true; private int i = 0; private ITestService.Stub serviceBinder = new ITestService.Stub() { @Override public int getCount() throws RemoteException { // TODO Auto-generated method stub return i; } }; @Override public IBinder onBind(Intent i) { Log.e(TAG, "============> TestService.onBind"); return serviceBinder; } @Override public boolean onUnbind(Intent i) { Log.e(TAG, "============> TestService.onUnbind"); return false; } @Override public void onRebind(Intent i) { Log.e(TAG, "============> TestService.onRebind"); } @Override public void onCreate() { Log.e(TAG, "============> TestService.onCreate"); _nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); showNotification(); new Thread() { public void run() { try { while (threadEnable) { sleep(1000); Log.d(TAG, "Count = " + (++i)); } } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); } @Override public void onStart(Intent intent, int startId) { Log.e(TAG, "============> TestService.onStart"); } @Override public void onDestroy() { _nm.cancel(R.string.service_started); threadEnable = false; Log.e(TAG, "============> TestService.onDestroy"); } private void showNotification() { Notification notification = new Notification(R.drawable.face_1, "Service started", System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TestServiceHolder.class), 0); // must set this for content view, or will throw a exception notification.setLatestEventInfo(this, "Test Service", "Service started", contentIntent); _nm.notify(R.string.service_started, notification); } public int getCount() { return i; }}
=======================================
ITestService.aidl package com.iceskysl.TestServiceHolder; interface ITestService{ int getCount();}
========================================
- Android 调用Remote Service方法
- Android Remote Service
- android remote service
- Android remote service
- Android 调用web service 方法
- android本地调用service方法
- Android Service:bindService与Remote Service
- android bind service and remote service(aidl)
- Local Service and Remote Service[Android]
- 【Service 1】Android Remote Service - AIDL
- Android service 之Remote Service详解
- android 支付宝集成问题 4.1以上系统不能调用 failure calling remote service 解决办法
- Android Service 调用服务中的方法
- Android Service 调用远程服务中的方法
- Android Service的onRebind方法调用时机
- Android remote service : how to bind
- Android remote service : how to bind
- android 进程与remote service的小记
- Hibernate中的3种对象状态
- java学习进阶
- EAS-BOS 应用框架编程指南
- 利用网络实现自己的六度人脉
- jspSmartUpload
- Android 调用Remote Service方法
- 开发者版本:你属于哪个版本的程序员?
- 几个常用的js参考手册和参考网站
- java se反射
- ZOJ1006
- 再议自动化框架
- Messenger:使用消息的跨进程通信
- Android图像处理资料
- RelaxInternet之Shared Photos to Relaxing Internet (Android Client)