Reputation: 139
Alright I got a good bit of help in my last question so instead of beat my head against the wall I have decided it would be better to post my code for my auto starter / service / manifest here and see what I can get. It goes like this: Auto starter(Just fixed) --> Service --> Main activity
So I am not so much worried about the first and last steps as they are both taken care of. I just need to figure out how to get the auto starter to run the service which ties the main activity to the background. So the question: Why is it that when I run this it starts the app then promptly crashes it about 30 seconds later also why is the app not going to the background instead of staying up front? here is the code:
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;
}
}
As well as the updated manifest:
<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>
Here is the starter as requested:
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);
}
}
}
Upvotes: 1
Views: 188
Reputation: 10472
Change Monitor to .Monitor in the manifest.
You should use startService() rather than startActivity().
Upvotes: 1