我是android新手。我正在尝试创建一个非常基本的后台服务。 Logcat的错误粘贴在下面。我想在我的标签上运行它。该应用程序打开然后变为空白,并显示消息“不幸的是,后台服务已停止”。任何帮助将不胜感激。
我的主要活动
package com.example.backgroundservice;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView view = new TextView(this);
view.setText("Service Test");
Intent i = new Intent();
i.setClassName("com.example.backgroundservice", "com.example.backgroundservice.MyService");
bindService(i, null, Context.BIND_AUTO_CREATE);
this.startService(i);
setContentView(view);
}
}
我的服务类
package com.example.backgroundservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
String tag="TestService";
@Override
public void onCreate(){
super.onCreate();
Toast.makeText(this, "Service Created..", Toast.LENGTH_LONG).show();
Log.e(tag, "Service Created..");
}
@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId){
super.onStart(intent, startId);
Log.e(tag, "Service started..");
}
@Override
public void onDestroy(){
super.onDestroy();
Toast.makeText(this, "Service destroyed..", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.backgroundservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service class="com.example.backgroundservice.MyService"
android:name="com.example.backgroundservice.MyService"
android:exported="false">
<intent-filter>
<action android:value="com.example.backgroundservice.MyService.MY_SERVICE"
android:name="com.example.backgroundservice.MyService" />
</intent-filter>
</service>
<activity
android:name="com.example.backgroundservice.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
LogCat o / p:
02-02 14:54:34.805: E/AndroidRuntime(18988): FATAL EXCEPTION: main
02-02 14:54:34.805: E/AndroidRuntime(18988): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.backgroundservice/com.example.backgroundservice.MainActivity}: java.lang.IllegalArgumentException: connection is null
02-02 14:54:34.805: E/AndroidRuntime(18988): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
02-02 14:54:34.805: E/AndroidRuntime(18988): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
02-02 14:54:34.805: E/AndroidRuntime(18988): at android.app.ActivityThread.access$700(ActivityThread.java:150)
02-02 14:54:34.805: E/AndroidRuntime(18988): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
02-02 14:54:34.805: E/AndroidRuntime(18988): at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 14:54:34.805: E/AndroidRuntime(18988): at android.os.Looper.loop(Looper.java:137)
02-02 14:54:34.805: E/AndroidRuntime(18988): at android.app.ActivityThread.main(ActivityThread.java:5279)
02-02 14:54:34.805: E/AndroidRuntime(18988): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 14:54:34.805: E/AndroidRuntime(18988): at java.lang.reflect.Method.invoke(Method.java:511)
02-02 14:54:34.805: E/AndroidRuntime(18988): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-02 14:54:34.805: E/AndroidRuntime(18988): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-02 14:54:34.805: E/AndroidRuntime(18988): at dalvik.system.NativeStart.main(Native Method)
02-02 14:54:34.805: E/AndroidRuntime(18988): Caused by: java.lang.IllegalArgumentException: connection is null
02-02 14:54:34.805: E/AndroidRuntime(18988): at android.app.ContextImpl.bindService(ContextImpl.java:1759)
02-02 14:54:34.805: E/AndroidRuntime(18988): at android.app.ContextImpl.bindService(ContextImpl.java:1751)
02-02 14:54:34.805: E/AndroidRuntime(18988): at android.content.ContextWrapper.bindService(ContextWrapper.java:480)
02-02 14:54:34.805: E/AndroidRuntime(18988): at com.example.backgroundservice.MainActivity.onCreate(MainActivity.java:18)
02-02 14:54:34.805: E/AndroidRuntime(18988): at android.app.Activity.performCreate(Activity.java:5276)
02-02 14:54:34.805: E/AndroidRuntime(18988): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
02-02 14:54:34.805: E/AndroidRuntime(18988): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
02-02 14:54:34.805: E/AndroidRuntime(18988): ... 11 more
谢谢大家的建议。我在bindService()中处理'Null'。我的服务现在工作正常,但我确实在跟踪中看到了以下内容。我没有得到它,非常感谢您在调试时的任何帮助。
跟踪:
02-02 19:04:05.120: E/ActivityThread(21488): Activity com.example.backgroundservice.MainActivity has leaked ServiceConnection com.example.backgroundservice.MainActivity$1@423d2220 that was originally bound here
02-02 19:04:05.120: E/ActivityThread(21488): android.app.ServiceConnectionLeaked: Activity com.example.backgroundservice.MainActivity has leaked ServiceConnection com.example.backgroundservice.MainActivity$1@423d2220 that was originally bound here
02-02 19:04:05.120: E/ActivityThread(21488): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:974)
02-02 19:04:05.120: E/ActivityThread(21488): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:868)
02-02 19:04:05.120: E/ActivityThread(21488): at android.app.ContextImpl.bindService(ContextImpl.java:1762)
02-02 19:04:05.120: E/ActivityThread(21488): at android.app.ContextImpl.bindService(ContextImpl.java:1751)
02-02 19:04:05.120: E/ActivityThread(21488): at android.content.ContextWrapper.bindService(ContextWrapper.java:480)
02-02 19:04:05.120: E/ActivityThread(21488): at com.example.backgroundservice.MainActivity.onCreate(MainActivity.java:26)
02-02 19:04:05.120: E/ActivityThread(21488): at android.app.Activity.performCreate(Activity.java:5276)
02-02 19:04:05.120: E/ActivityThread(21488): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
02-02 19:04:05.120: E/ActivityThread(21488): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
02-02 19:04:05.120: E/ActivityThread(21488): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
02-02 19:04:05.120: E/ActivityThread(21488): at android.app.ActivityThread.access$700(ActivityThread.java:150)
02-02 19:04:05.120: E/ActivityThread(21488): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
02-02 19:04:05.120: E/ActivityThread(21488): at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 19:04:05.120: E/ActivityThread(21488): at android.os.Looper.loop(Looper.java:137)
02-02 19:04:05.120: E/ActivityThread(21488): at android.app.ActivityThread.main(ActivityThread.java:5279)
02-02 19:04:05.120: E/ActivityThread(21488): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 19:04:05.120: E/ActivityThread(21488): at java.lang.reflect.Method.invoke(Method.java:511)
02-02 19:04:05.120: E/ActivityThread(21488): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-02 19:04:05.120: E/ActivityThread(21488): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-02 19:04:05.120: E/ActivityThread(21488): at dalvik.system.NativeStart.main(Native Method)
02-02 19:04:30.075: E/TestService(21488): Service started..
02-02 19:04:31.850: D/GestureDetector(21488): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 10 mFalseSizeCnt:0
02-02 19:04:32.750: D/AbsListView(21488): Get MotionRecognitionManager
02-02 19:04:34.145: E/ActivityThread(21488): Activity com.example.backgroundservice.MainActivity has leaked ServiceConnection com.example.backgroundservice.MainActivity$1@4241f808 that was originally bound here
02-02 19:04:34.145: E/ActivityThread(21488): android.app.ServiceConnectionLeaked: Activity com.example.backgroundservice.MainActivity has leaked ServiceConnection com.example.backgroundservice.MainActivity$1@4241f808 that was originally bound here
02-02 19:04:34.145: E/ActivityThread(21488): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:974)
02-02 19:04:34.145: E/ActivityThread(21488): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:868)
02-02 19:04:34.145: E/ActivityThread(21488): at android.app.ContextImpl.bindService(ContextImpl.java:1762)
02-02 19:04:34.145: E/ActivityThread(21488): at android.app.ContextImpl.bindService(ContextImpl.java:1751)
02-02 19:04:34.145: E/ActivityThread(21488): at android.content.ContextWrapper.bindService(ContextWrapper.java:480)
02-02 19:04:34.145: E/ActivityThread(21488): at com.example.backgroundservice.MainActivity.onCreate(MainActivity.java:26)
02-02 19:04:34.145: E/ActivityThread(21488): at android.app.Activity.performCreate(Activity.java:5276)
02-02 19:04:34.145: E/ActivityThread(21488): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
02-02 19:04:34.145: E/ActivityThread(21488): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
02-02 19:04:34.145: E/ActivityThread(21488): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
02-02 19:04:34.145: E/ActivityThread(21488): at android.app.ActivityThread.access$700(ActivityThread.java:150)
02-02 19:04:34.145: E/ActivityThread(21488): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
02-02 19:04:34.145: E/ActivityThread(21488): at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 19:04:34.145: E/ActivityThread(21488): at android.os.Looper.loop(Looper.java:137)
02-02 19:04:34.145: E/ActivityThread(21488): at android.app.ActivityThread.main(ActivityThread.java:5279)
02-02 19:04:34.145: E/ActivityThread(21488): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 19:04:34.145: E/ActivityThread(21488): at java.lang.reflect.Method.invoke(Method.java:511)
02-02 19:04:34.145: E/ActivityThread(21488): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-02 19:04:34.145: E/ActivityThread(21488): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-02 19:04:34.145: E/ActivityThread(21488): at dalvik.system.NativeStart.main(Native Method)
02-02 19:05:39.800: E/TestService(21488): Service Created..
02-02 19:05:39.800: E/TestService(21488): Service started..
02-02 19:05:44.795: E/ActivityThread(21488): Activity com.example.backgroundservice.MainActivity has leaked ServiceConnection com.example.backgroundservice.MainActivity$1@424cf1d8 that was originally bound here
02-02 19:05:44.795: E/ActivityThread(21488): android.app.ServiceConnectionLeaked: Activity com.example.backgroundservice.MainActivity has leaked ServiceConnection com.example.backgroundservice.MainActivity$1@424cf1d8 that was originally bound here
02-02 19:05:44.795: E/ActivityThread(21488): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:974)
02-02 19:05:44.795: E/ActivityThread(21488): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:868)
02-02 19:05:44.795: E/ActivityThread(21488): at android.app.ContextImpl.bindService(ContextImpl.java:1762)
02-02 19:05:44.795: E/ActivityThread(21488): at android.app.ContextImpl.bindService(ContextImpl.java:1751)
02-02 19:05:44.795: E/ActivityThread(21488): at android.content.ContextWrapper.bindService(ContextWrapper.java:480)
02-02 19:05:44.795: E/ActivityThread(21488): at com.example.backgroundservice.MainActivity.onCreate(MainActivity.java:26)
02-02 19:05:44.795: E/ActivityThread(21488): at android.app.Activity.performCreate(Activity.java:5276)
02-02 19:05:44.795: E/ActivityThread(21488): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
02-02 19:05:44.795: E/ActivityThread(21488): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
02-02 19:05:44.795: E/ActivityThread(21488): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
02-02 19:05:44.795: E/ActivityThread(21488): at android.app.ActivityThread.access$700(ActivityThread.java:150)
02-02 19:05:44.795: E/ActivityThread(21488): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
02-02 19:05:44.795: E/ActivityThread(21488): at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 19:05:44.795: E/ActivityThread(21488): at android.os.Looper.loop(Looper.java:137)
02-02 19:05:44.795: E/ActivityThread(21488): at android.app.ActivityThread.main(ActivityThread.java:5279)
02-02 19:05:44.795: E/ActivityThread(21488): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 19:05:44.795: E/ActivityThread(21488): at java.lang.reflect.Method.invoke(Method.java:511)
02-02 19:05:44.795: E/ActivityThread(21488): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-02 19:05:44.795: E/ActivityThread(21488): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-02 19:05:44.795: E/ActivityThread(21488): at dalvik.system.NativeStart.main(Native Method)
修改过的服务类
package com.example.backgroundservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
String tag="TestService";
@Override
public void onCreate(){
super.onCreate();
Toast.makeText(this, "Service Created..", Toast.LENGTH_LONG).show();
Log.e(tag, "Service Created..");
}
@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId){
super.onStart(intent, startId);
Log.e(tag, "Service started..");
}
@Override
public void onDestroy(){
super.onDestroy();
Toast.makeText(this, "Service destroyed..", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
class LocalBinder extends Binder {
MyService getService() {
// Return this instance of LocalService so clients can call public methods
return MyService.this;
}
}
}
修改后的主要活动类
package com.example.backgroundservice;
import com.example.backgroundservice.MyService.LocalBinder;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.widget.TextView;
public class MainActivity extends Activity {
MyService mService;
boolean mBound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView view = new TextView(this);
view.setText("Service Test");
Intent i = new Intent();
i.setClassName("com.example.backgroundservice", "com.example.backgroundservice.MyService");
bindService(i, mConnection, Context.BIND_AUTO_CREATE);
this.startService(i);
setContentView(view);
}
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;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
}
答案 0 :(得分:2)
基本上你拥有这一切,在这一行:
bindService(i,null,Context.BIND_AUTO_CREATE);
conn参数不能为null:
conn在服务启动和停止时接收信息。这个 必须是有效的ServiceConnection对象;它不能为空。
您可以在此处找到绑定服务的完整示例:
http://developer.android.com/guide/components/bound-services.html
答案 1 :(得分:0)
类似文档说link do tocs
第二个参数不能为空ServiceConnection conn,
“在服务启动和停止时接收信息。这必须是有效的ServiceConnection对象;它不能为空。”
答案 2 :(得分:0)
Android服务可以采用两种形式:启动和绑定。启动服务是通过调用startService()
方法启动的,通常用于执行单个操作。当然,他们也可能在后台无限期地运行。通过调用stopSelf()
完成操作后,此类服务可能会自行停止。如果您想使用此类服务,请删除以下行以消除异常:bindService(i, null, Context.BIND_AUTO_CREATE);
请注意,API级别5中已弃用onStart()
回调。您应该使用onStartCommand(Intent intent, int flags, int startId)
代替。