我有一个服务,MyService
,我永远在一个小的测试应用程序中使用一个简单的按钮启动,具体如下(来源整个MainActivity):服务启动时Android应用程序崩溃了吗?
package com.example.testapp;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startButton = (Button) findViewById(R.id.button1);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MyService.class);
startService(intent);
}
});
Button stopButton = (Button) findViewById(R.id.button2);
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这似乎做工精细 - 服务开始,卵石从我的android应用程序接收它需要的数据。
但是,我希望这个服务在启动时在后台运行,而不是简单地在应用程序启动时运行。这样做的方式,显然,正在申报启动权限清单文件与服务一起:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<service android:name="com.example.testapp.MyService" android:exported="false"/>
除了声明BroadcastReceiver
:
public class OnStartReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, MyService.class);
context.startService(myIntent);
}
}
假设这是当我尝试从Pebble(通过Pebble)从Android设备获取数据时,在重新启动我的Android设备并给予一切时间加载(并确保Pebble已连接到我的手机)时正确实施启动服务,本质上,与Service
交互),测试应用程序崩溃。然后我进入设置 - >应用程序,并发现虽然我的测试应用程序的进程崩溃了,但该服务仍在运行,尽管它不像预期的那样运行,并且可能只是死了。
我不知道为什么这可能是问题。如果有人有任何建议,我很乐意听到他们。
谢谢!
编辑:
相关的logcat输出:
V/Zygote (2572): Switching descriptor 33 to /dev/null
V/Zygote (2572): Switching descriptor 9 to /dev/null
D/dalvikvm(2572): Late-enabling CheckJNI
I/ActivityManager( 736): Start proc com.example.testapp for broadcast com.example.testapp/.OnStartReceiver: pid=2572 uid=10144 gids={50144}
D/ActivityThread(2572): handleBindApplication:com.example.testapp
D/ActivityThread(2572): setTargetHeapUtilization:0.75
D/ActivityThread(2572): setTargetHeapMinFree:524288
I/System.out(2572): service is started
D/AndroidRuntime(2572): Shutting down VM
W/dalvikvm(2572): threadid=1: thread exiting with uncaught exception (group=0x415dcce0)
E/AndroidRuntime(2572): FATAL EXCEPTION: main
E/AndroidRuntime(2572): Process: com.example.testapp, PID: 2572
E/AndroidRuntime(2572): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.getpebble.action.app.RECEIVE_NACK flg=0x10 (has extras) } in [email protected]
E/AndroidRuntime(2572): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:785)
E/AndroidRuntime(2572): at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime(2572): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(2572): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(2572): at android.app.ActivityThread.main(ActivityThread.java:5149)
E/AndroidRuntime(2572): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(2572): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(2572): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
E/AndroidRuntime(2572): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
E/AndroidRuntime(2572): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(2572): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=60; index=-1
E/AndroidRuntime(2572): at java.util.ArrayList.get(ArrayList.java:310)
E/AndroidRuntime(2572): at com.example.testapp.MyService$1$2.receiveNack(MyService.java:184)
E/AndroidRuntime(2572): at com.getpebble.android.kit.PebbleKit$PebbleNackReceiver.onReceive(PebbleKit.java:627)
E/AndroidRuntime(2572): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:775)
E/AndroidRuntime(2572): ... 9 more
I/Process (2572): Sending signal. PID: 2572 SIG: 9
I/ActivityManager( 736): Process com.example.testapp (pid 2572) has died.
您需要提供logcat输出和代码。这听起来像是测试应用程序有问题,而不是服务。 –
我将使用代码更新原始帖子。但是,当我的Android设备重新启动后我的应用崩溃时,我似乎没有在ADT/eclipse中获得任何logcat输出。 – Vasu
您可能不得不在命令行中使用adb logcat。 Eclipse会将你的logcat输出过滤到你正在运行的应用程序。如果某些东西崩溃了,它会在logcat中显示堆栈跟踪和详细信息。 –