onServiceConnected,onServiceDisconnected

①Service中需要创建一个实现IBinder的内部类(这个内部类不一定在Service中实现,但必须在Service中创建它)。
在OnBind()方法中需返回一个IBinder实例,不然onServiceConnected方法不会调用。


②ServiceConnection 的回调方法onServiceDisconnected() 在连接正常关闭的情况下是不会被调用的, 该方法只在Service 被破坏了或者被杀死的时候调用. 例如, 系统资源不足, 要关闭一些Services, 刚好连接绑定的 Service 是被关闭者之一,  这个时候onServiceDisconnected() 就会被调用。


 

Android IPC通信bindService不能回调onServiceConnected方法

10-16

Server端:rn用aidl定义了一个接口,接口中有个add方法,然后写了一个Service实现了这个接口rn[code=java]rnpublic class CalculateService extends Service rn private static final String TAG = "CalculateService";rnrn @Overridern public void onCreate() rn super.onCreate();rn rnrn @Overridern public IBinder onBind(Intent arg0) rn // TODO Auto-generated method stubrn Log.d(TAG, "onBind()");rn return mBinder;rn rnrn private final ICalculateInterface.Stub mBinder = new ICalculateInterface.Stub() rnrn @Overridern public int add(int a, int b) throws RemoteException rn Log.d(TAG, "远程计算中...");rn Log.d(TAG, a + " + " + b + " = " + (a + b));rnrn return a + b;rn rnrn @Overridern public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException rnrn rn ;rnrn[/code]rn在AndroidManifest.xml中注册了服务rn[code=html]rnrn rn rn rn rn[/code]rnClient端rn[code=java]rnpublic class MainActivity extends AppCompatActivity rn private final String TAG = "MainActivity";rn private EditText num1 = null;rn private EditText num2 = null;rn private TextView result = null;rn private Button btn = null;rn private ICalculateInterface calculateInterface = null;rnrn ServiceConnection serviceConnection = new ServiceConnection() rnrn @Overridern public void onServiceConnected(ComponentName name, IBinder service) rn calculateInterface = ICalculateInterface.Stub.asInterface(service);rn if (calculateInterface == null) rn Log.d(TAG, "calculateInterface 对象为空");rn else rn Log.d(TAG, "获取 calculateInterface 对象");rn rn rnrn @Overridern public void onServiceDisconnected(ComponentName name) rn calculateInterface = null;rn rn ;rnrn @Overridern protected void onCreate(Bundle savedInstanceState) rn super.onCreate(savedInstanceState);rn setContentView(R.layout.activity_main);rnrn num1 = (EditText) findViewById(R.id.et1);rn num2 = (EditText) findViewById(R.id.et2);rn result = (TextView) findViewById(R.id.tv4);rn btn = (Button) findViewById(R.id.btn);rnrn btn.setOnClickListener(new ButtonClickListener());rn rnrn class ButtonClickListener implements View.OnClickListener rnrn @Overridern public void onClick(View v) rn Intent intent = new Intent("CalculateService");rn Intent eintent = new Intent(createExplicitFromImplicitIntent(MainActivity.this, intent));rn bindService(eintent, serviceConnection, Context.BIND_AUTO_CREATE);rn if (calculateInterface == null)rn Log.d(TAG, "未连接服务器");rn try rn int res = calculateInterface.add(Integer.parseInt(num1.getText().toString()), Integer.parseInt(num2.getText().toString()));rn result.setText(res + "");rn catch (RemoteException e) rn e.printStackTrace();rn rn rn rnrn public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) rn // Retrieve all services that can match the given intentrn PackageManager pm = context.getPackageManager();rn List resolveInfo = pm.queryIntentServices(implicitIntent, 0);rnrn // Make sure only one match was foundrn if (resolveInfo == null || resolveInfo.size() != 1) rn return null;rn rnrn // Get component info and create ComponentNamern ResolveInfo serviceInfo = resolveInfo.get(0);rn String packageName = serviceInfo.serviceInfo.packageName;rn String className = serviceInfo.serviceInfo.name;rn ComponentName component = new ComponentName(packageName, className);rnrn // Create a new intent. Use the old one for extras and such reusern Intent explicitIntent = new Intent(implicitIntent);rnrn // Set the component to be explicitrn explicitIntent.setComponent(component);rnrn return explicitIntent;rn rnrn[/code]rn运行的时候int res = calculateInterface.add那行报错:rnjava.lang.NullPointerException: Attempt to invoke interface method 'int com.cumt.aidl.ICalculateInterface.add(int, int)' on a null object referencern打印“未连接服务器”,onServiceConnected中的代码也没有执行rn求大神帮忙看看我哪里学错了,或者少些了什么??

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭