按照 Android 开发者网站教程中的说明进行操作时,无法绑定到服务
尽管我按照 Android 开发人员网站上的教程进行操作,但我无法将我的活动绑定到我的服务。
package com.dieselboris.motioncapture;
import android.app.Activity;
import android.app.AlertDialog;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.dieselboris.motioncapture.LocalService.LocalBinder;
public class HelloMotionCaptureActivity extends Activity implements OnClickListener {
private TextView textBox;
LocalService mService;
boolean mBound = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textBox = (TextView) findViewById(R.id.textView1);
}
@Override
protected void onStart() {
super.onStart();
// Bind to LocalService NEVER RETURNS TRUE!!!!
Intent intent = new Intent(this, LocalService.class);
boolean isBound = bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
String text = Boolean.toString(isBound);
textBox.setText(text);
}
@Override
protected void onStop() {
super.onStop();
// Unbind from the service
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
//start the service when clicked
public void onClick(View v) {
if (v == startButton)
{
//NEVER IN HERE
if (mBound) {
int num = mService.getRandomNumber();
Toast.makeText(this, "incoming number: " + num, Toast.LENGTH_LONG).show();
}
Toast.makeText(this, "the service should run now", Toast.LENGTH_LONG).show();
}
}
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
if (mBound) {
// Call a method from the LocalService.
// However, if this call were something that might hang, then this request should
// occur in a separate thread to avoid slowing down the activity performance.
int num = mService.getRandomNumber();
String text = Integer.toString(num);
textBox.setText(text);
}
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
}
我尝试连接的服务:
package com.dieselboris.motioncapture;
import java.util.Random;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class LocalService extends Service {
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
// Random number generator
private final Random mGenerator = new Random();
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
LocalService getService() {
// Return this instance of LocalService so clients can call public methods
return LocalService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
/** method for clients */
public int getRandomNumber() {
return mGenerator.nextInt(100);
}
}
我从模拟器获得以下输出:
错误/AndroidRuntime(10878):致命异常:main
错误/AndroidRuntime(10878): java.lang.NullPointerException
错误/AndroidRuntime(10878):位于 com.dieselboris.motioncapture.HelloMotionCaptureActivity.onClick(HelloMotionCaptureActivity.java:66)
错误/AndroidRuntime(10878):位于 android.view.View.performClick(View.java:2408)
错误/AndroidRuntime(10878):位于 android.view.View$PerformClick.run(View.java:8817)
错误/AndroidRuntime(10878):在 android.os.Handler.handleCallback(Handler.java:587)
错误/AndroidRuntime(10878):位于 android.os.Handler.dispatchMessage(Handler.java:92)
错误/AndroidRuntime(10878):位于 android.os.Looper.loop(Looper.java:144)
错误/AndroidRuntime(10878):位于 android.app.ActivityThread.main(ActivityThread.java:4937)
错误/AndroidRuntime(10878):位于 java.lang.reflect.Method.invokeNative(本机方法)
错误/AndroidRuntime(10878):位于 java.lang.reflect.Method.invoke(Method.java:521)
错误/AndroidRuntime(10878):位于 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
错误/AndroidRuntime(10878):位于 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
错误/AndroidRuntime(10878):在dalvik.system.NativeStart.main(本机方法)
看来我在初始化或连接尚未设置之前从服务中调用了公共方法。
我仔细地按照开发人员网站上的示例进行操作,但无法弄清楚我做错了什么。
Although I followed the tutorial on the Android Developers website, I can't get my activity to bind to my service.
package com.dieselboris.motioncapture;
import android.app.Activity;
import android.app.AlertDialog;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.dieselboris.motioncapture.LocalService.LocalBinder;
public class HelloMotionCaptureActivity extends Activity implements OnClickListener {
private TextView textBox;
LocalService mService;
boolean mBound = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textBox = (TextView) findViewById(R.id.textView1);
}
@Override
protected void onStart() {
super.onStart();
// Bind to LocalService NEVER RETURNS TRUE!!!!
Intent intent = new Intent(this, LocalService.class);
boolean isBound = bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
String text = Boolean.toString(isBound);
textBox.setText(text);
}
@Override
protected void onStop() {
super.onStop();
// Unbind from the service
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
//start the service when clicked
public void onClick(View v) {
if (v == startButton)
{
//NEVER IN HERE
if (mBound) {
int num = mService.getRandomNumber();
Toast.makeText(this, "incoming number: " + num, Toast.LENGTH_LONG).show();
}
Toast.makeText(this, "the service should run now", Toast.LENGTH_LONG).show();
}
}
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
if (mBound) {
// Call a method from the LocalService.
// However, if this call were something that might hang, then this request should
// occur in a separate thread to avoid slowing down the activity performance.
int num = mService.getRandomNumber();
String text = Integer.toString(num);
textBox.setText(text);
}
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
}
The Service I try to connect to:
package com.dieselboris.motioncapture;
import java.util.Random;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class LocalService extends Service {
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
// Random number generator
private final Random mGenerator = new Random();
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
LocalService getService() {
// Return this instance of LocalService so clients can call public methods
return LocalService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
/** method for clients */
public int getRandomNumber() {
return mGenerator.nextInt(100);
}
}
I get the following output from the emulator:
ERROR/AndroidRuntime(10878): FATAL EXCEPTION: main
ERROR/AndroidRuntime(10878): java.lang.NullPointerException
ERROR/AndroidRuntime(10878): at com.dieselboris.motioncapture.HelloMotionCaptureActivity.onClick(HelloMotionCaptureActivity.java:66)
ERROR/AndroidRuntime(10878): at android.view.View.performClick(View.java:2408)
ERROR/AndroidRuntime(10878): at android.view.View$PerformClick.run(View.java:8817)
ERROR/AndroidRuntime(10878): at android.os.Handler.handleCallback(Handler.java:587)
ERROR/AndroidRuntime(10878): at android.os.Handler.dispatchMessage(Handler.java:92)
ERROR/AndroidRuntime(10878): at android.os.Looper.loop(Looper.java:144)
ERROR/AndroidRuntime(10878): at android.app.ActivityThread.main(ActivityThread.java:4937)
ERROR/AndroidRuntime(10878): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(10878): at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(10878): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
ERROR/AndroidRuntime(10878): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
ERROR/AndroidRuntime(10878): at dalvik.system.NativeStart.main(Native Method)
It seems that I call the public method from the service before it's initialized or the connection is not yet setup.
I followed the example on the developers site carefully and can't figure out what I am doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的权限设置不正确。
我必须在应用程序清单中插入整个应用程序的权限和“
uses-permission
”。如果没有权限,意图永远不会传递给服务。
My permissions weren't set correctly.
I had to insert a permission for the whole application and a "
uses-permission
" in application manifest.Without permissions the intent is never delivered to the service.