Android 服务启动,然后崩溃
好的,在我的最后一个问题中我得到了一些帮助,所以我决定最好在这里发布我的auto starter/service/manifest代码,看看我能得到什么。 它是这样的:Auto starter(刚刚修复)-->Service-->主活动 因此,我不太担心第一步和最后一步,因为它们都得到了处理。我只需要弄清楚如何让auto starter运行将主要活动绑定到后台的服务。所以问题是:为什么当我运行这个程序时,它会启动应用程序,然后在大约30秒后立即崩溃?为什么应用程序不会进入后台,而不是停留在前端? 代码如下:Android 服务启动,然后崩溃,android,service,Android,Service,好的,在我的最后一个问题中我得到了一些帮助,所以我决定最好在这里发布我的auto starter/service/manifest代码,看看我能得到什么。 它是这样的:Auto starter(刚刚修复)-->Service-->主活动 因此,我不太担心第一步和最后一步,因为它们都得到了处理。我只需要弄清楚如何让auto starter运行将主要活动绑定到后台的服务。所以问题是:为什么当我运行这个程序时,它会启动应用程序,然后在大约30秒后立即崩溃?为什么应用程序不会进入后台,而不是停留在前端?
package path.to.file;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class Monitor extends Service {
private static final String LOG_TAG = "::Monitor";
@Override
public void onCreate() {
super.onCreate();
Log.e(LOG_TAG, "Service created.");
Intent i = new Intent();
i.setAction("path.to.file.MainActivity");
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
}
@Override
public void onStart(Intent intent, int startId) {
super.onStartCommand(intent, startId, startId);
Log.e(LOG_TAG, "Service started.");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(LOG_TAG, "Service destroyed.");
}
@Override
public IBinder onBind(Intent intent) {
Log.e(LOG_TAG, "Service bind.");
return null;
}
}
以及更新的清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
installlocation="internalOnly"
package="path.to.file"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="Monitor" >
<intent-filter>
<action android:name="path.to.file.Monitor" >
</action>
</intent-filter>
</service>
<receiver android:name="autoBot" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
</application>
</manifest>
将清单中的监视器更改为.Monitor
您应该使用startService()而不是startActivity()。将清单中的监视器更改为.Monitor
您应该使用startService()而不是startActivity()。如果应用程序崩溃,请发布日志猫,以便我们可以看到发生了什么。@Sam这很不幸,因为我正在用手机运行此功能,无法访问日志猫。给我20分钟让emu启动,我会给你回复的。如果你的手机连接到你的电脑,你从Eclipse运行你的应用程序,那么你只会在模拟器上得到logcat跟踪。@Sam你是什么意思?只需将usb插入手机,然后在手机上运行应用程序?更正,重新启动手机后,它在X个时间段后不再停止,但它不会像我尝试的那样在后台运行。如果应用程序崩溃,请发布logcat,以便我们看到发生了什么。@Sam这很不幸,因为我正在用手机运行此功能,无法访问logcat。给我20分钟让emu启动,我会给你回复的。如果你的手机连接到你的电脑,你从Eclipse运行你的应用程序,那么你只会在模拟器上得到logcat跟踪。@Sam你是什么意思?只需将usb插入手机,然后在我的手机上运行应用程序?更正,重新启动手机后,它在X时间后不再停止,但它不会在后台运行,因为我正试图让它运行。代码,我将请求的代码添加到页面顶部。我希望这能多帮点忙。好的。我看到您使用的是startActivity(),如果您试图启动一个服务,那么应该是startService(I)。我相信Monitor.class指的是您的服务。因此,请使用startService(),意图指向Monitor.class。如果另一方面,您试图启动一个活动,您仍然需要使用正确的名称MainActivity.class,而不是Monitor.class。问题:我还需要一个主活动吗。在我看来,auto starter充当主启动器,因此我不需要清单代码中的“启动器”,我将请求的代码添加到页面顶部。我希望这能多帮点忙。好的。我看到您使用的是startActivity(),如果您试图启动一个服务,那么应该是startService(I)。我相信Monitor.class指的是您的服务。因此,请使用startService(),意图指向Monitor.class。如果另一方面,您试图启动一个活动,您仍然需要使用正确的名称MainActivity.class,而不是Monitor.class。问题:我还需要一个主活动吗。在我看来,自动启动器充当主启动器,因此我真的不需要清单中的“启动器”吗?
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class autoBot extends BroadcastReceiver {
private static final String LOG_TAG = "StartAtBootServiceReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.e(LOG_TAG, "onReceive:");
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent(context, Monitor.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}