带IPC的Android服务
我花了两天时间想弄明白这一点。我有一个我正在创建的服务,用于实践服务和带IPC的Android服务,android,Android,我花了两天时间想弄明白这一点。我有一个我正在创建的服务,用于实践服务和IPC。我已经对照我正在读的书检查了代码,几乎完全一样,唯一的区别是标识符。我在mainwindow.java文件中抛出了一个ResourcesNotFoundException。当我尝试使用我的IBinder时,它会被抛出。任何方向正确的提示都将不胜感激 mainwindow.java package com.evilOrion; import com.evilOrion.R; import android.app.Acti
IPC
。我已经对照我正在读的书检查了代码,几乎完全一样,唯一的区别是标识符。我在mainwindow.java
文件中抛出了一个ResourcesNotFoundException
。当我尝试使用我的IBinder
时,它会被抛出。任何方向正确的提示都将不胜感激
mainwindow.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);
}
}
}
AdderService.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>
奥卡姆剃刀的效果。有时你吃酒吧,有时酒吧吃你
tView
不接受int值
答复:
tView.setText(String.valueOf(result);
天哪,一个星期后我才发现这一点
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
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.setText(String.valueOf(result);