使用IPC的Android服务

问题描述 投票:0回答:1

我花了最近两天试图解决这个问题。我有一项服务,我正在创建服务和IPC练习。我已经检查了我正在阅读它的书的代码几乎是唯一的区别是标识符。我在ResourcesNotFoundException文件中输入了mainwindow.java。当我尝试使用我的IBinder时,它被抛出。任何正确方向的提示将非常感激。

main window.Java

package com.evilOrion;
import com.evilOrion.R;
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.DeadObjectException;
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.TextView;

public class mainwindow extends Activity {
    TextView tView = null;
    Button tButton = null;
    private adder service;
    private boolean bound;

    private ServiceConnection connection = new ServiceConnection(){
        public void onServiceConnected(ComponentName className, IBinder iservice){
            service = adder.Stub.asInterface(iservice);
            bound = true;
        }
        public void onServiceDisconnected(ComponentName className){
            bound = false;
            service = null;
        }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tView = (TextView)findViewById(R.id.TextView);
        tView.setText("hi");

        tButton = (Button)findViewById(R.id.Button01);
        tButton.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(v.equals(tButton)){
                    try{
                        int result = service.add(5, 7);// crashes on this line with no exception
                        tView.setText(result);
                    }catch(DeadObjectException e){
                        Log.i("DeadObjectException", "found the problem"); 
                    }

                    catch(RemoteException e){
                        Log.i("Exception: " , "shoot!");
                    }
                    //tView.setText("What a wonderful day it is");
                }
            }
        });
    }
    public void onStart(){
        super.onStart();
        if(!bound){
            this.bindService(new Intent(mainwindow.this, AdderService.class), connection, Context.BIND_AUTO_CREATE);
        }
    }
    public void onPause(){
        super.onPause();
        if(bound){
            bound = false;
            this.unbindService(connection);
        }
    }
}

add而service.Java

package com.evilOrion;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

public class AdderService extends Service {

    private final adder.Stub binder = new adder.Stub() {

        @Override
        public int subtranct(int a, int B) throws RemoteException {
            // TODO Auto-generated method stub
            return  a - b;
        }

        @Override
        public String echo(String input) throws RemoteException {
            // TODO Auto-generated method stub
            return "echo" + input;
        }

        @Override
        public int add(int a, int B) throws RemoteException {
            // TODO Auto-generated method stub
            return  a + b;
        }
    };

    public IBinder onBind(Intent it){
        return this.binder;
    }
}

表现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.evilOrion"
    android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".mainwindow"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".AdderService"
    android:label="@string/app_name"></service>
    </application>
</manifest> 

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView   
        android:id="@+id/TextView"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"/>
    <Button
        android:id="@+id/Button01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Click Here"></Button>
</LinearLayout>

我的Manifest文件确实有<service>所以我相信它不存在。我在书和代码之间逐字排列,我看到0差异。我只是不明白我做错了什么。另一个奇怪的问题。什么创造了连接?我得到ServiceConnection保存关于我与onServiceConnectedonServiceDisconnected两种方法的连接的信息,但是我使用连接的唯一地方是在onStart(),在所有其他方法完成后onStart()会自动调用。基本上,我迷失了我通常避免提问,但我没有找到这个答案。

int result = service.add(5, 7); in the mainwindow.java file causes:
No package identifier when getting value for resource number 0x0000000c
09-14 20:36:08.684: INFO/Exception!!!(12446): android.content.res.Resources$NotFoundException: String resource ID #0xc
is thrown in the mainwindow.java
android
1个回答
0
投票
try{
    int result = service.add(5, 7);// crashes on this line with no exception
    tView.setText(result);
}catch(DeadObjectException e){
    Log.i("DeadObjectException", "found the problem"); 
}

奥卡姆的剃刀有效。有时你会吃酒吧,有时酒吧会吃你。

tView不接受int值。

回答:

tView.setText(String.valueOf(result); 

男孩我一周都觉得羞怯。

© www.soinside.com 2019 - 2024. All rights reserved.