空指針異常
空指針的問題咋解決,我換了action方式也不行.startService取不到服務(wù)器里面的Service類 startService(new Intent(this,Class.forName("aidl.msd.com.aidldemo.AidlService")));像這樣也不行
空指針的問題咋解決,我換了action方式也不行.startService取不到服務(wù)器里面的Service類 startService(new Intent(this,Class.forName("aidl.msd.com.aidldemo.AidlService")));像這樣也不行
2018-02-28
舉報
2018-03-04
另外服務(wù)端要注冊服務(wù),并且允許遠(yuǎn)程,不然會發(fā)生安全性異常
<service android:name=".MService"
? ? ? ? android:process=":remote"
? ? ? ? android:exported="true"
? ?/>
2018-03-04
package com.jin.myapplication;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.jin.appspare.AidlObj;
import com.jin.appspare.IMyAidlInterface;
import com.jin.appspare.Person;
import java.util.List;
public class MainActivity extends AppCompatActivity {
? ?private EditText mEt1;
? ?private EditText mEt2;
? ?private TextView mTv;
? ?private Button mBt;
? ?private IMyAidlInterface iMyAidlInterface;
? ?private AidlObj aidlObj;
? ?private ServiceConnection conn = new ServiceConnection() {
? ? ? ?@Override
? ? ? ?public void onServiceConnected(ComponentName name, IBinder service) {
? ? ? ? ? ?Log.e("client", "服務(wù)已經(jīng)連接" + name);
? ? ? ? ? ?//拿到遠(yuǎn)程的服務(wù)-----------------------------------------------
// ? ? ? ? ? ?iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
? ? ? ? ? ?aidlObj = AidlObj.Stub.asInterface(service);
? ? ? ?}
? ? ? ?@Override
? ? ? ?public void onServiceDisconnected(ComponentName name) {
? ? ? ? ? ?Log.e("client", "服務(wù)已經(jīng)斷開" + name);
? ? ? ? ? ?aidlObj = null;
? ? ? ?}
? ?};
? ?@Override
? ?protected void onCreate(Bundle savedInstanceState) {
? ? ? ?super.onCreate(savedInstanceState);
? ? ? ?setContentView(R.layout.activity_main);
? ? ? ?initView();
? ? ? ?bindService();
? ? ? ?initEvent();
? ?}
? ?private void initView() {
? ? ? ?mEt1 = (EditText) findViewById(R.id.editText);
? ? ? ?mEt2 = (EditText) findViewById(R.id.editText2);
? ? ? ?mTv = (TextView) findViewById(R.id.textView);
? ? ? ?mBt = (Button) findViewById(R.id.button);
? ?}
? ?private void initEvent() {
? ? ? ?mBt.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onClick(View v) {
// ? ? ? ? ? ? ? ?try {
// ? ? ? ? ? ? ? ? ? ?int number1 = Integer.parseInt(mEt1.getText().toString());
// ? ? ? ? ? ? ? ? ? ?int number2 = Integer.parseInt(mEt2.getText().toString());
// ? ? ? ? ? ? ? ? ? ?int add = iMyAidlInterface.add(number1, number2);
// ? ? ? ? ? ? ? ? ? ?mTv.setText(add + "");
// ? ? ? ? ? ? ? ?}catch (Exception e) {
// ? ? ? ? ? ? ? ? ? ?e.printStackTrace();
// ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?try {
????????????????//---------------------------------------------------------------------
? ? ? ? ? ? ? ? ?? List<Person> abc = aidlObj.add(new Person("ABC", 20));
? ? ? ? ? ? ? ? ? ?for (Person person : abc) {
? ? ? ? ? ? ? ? ? ? ? ?Log.e("person-name", person.getName());
? ? ? ? ? ? ? ? ? ? ? ?Log.e("person-age", person.getAge() + "");
? ? ? ? ? ? ? ? ? ?}
????????????//---------------------------------------------------------------------
? ? ? ? ? ? ?? } catch (RemoteException e) {
? ? ? ? ? ? ? ? ? ?e.printStackTrace();
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ?});
? ?}
? ?private void bindService() {
? ? ? ?//獲取服務(wù)端
? ? ? ?Intent intent = new Intent();
? ? ? ?//顯示Intent啟動綁定服務(wù)
? ? ? ?intent.setComponent(new ComponentName("com.jin.appspare","com.jin.appspare.MService"));
? ? ? ?//一綁定,就會啟動
? ? ? ?Log.e("client","開始綁定服務(wù)端");
? ? ? ?bindService(intent, conn, Context.BIND_AUTO_CREATE);
? ?}
? ?@Override
? ?protected void onDestroy() {
? ? ? ?super.onDestroy();
? ? ? ?unbindService(conn);
? ?}
}