我有一个从引导启动的应用程序。但它正在崩溃。当我从一个按钮启动它时,服务就可以工作了。我有所有的许可..
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.my.app.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>
<service android:enabled="true" android:name=".StartUpService" />
<receiver
android:name="com.my.app.StartUpReceiver"
android:enabled="true"
android:exported="false" >
</receiver>
<receiver android:name="com.my.app.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
引导接收器:
package com.my.app;
import java.util.Calendar;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
Intent i = new Intent(context, StartUpReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.cancel(pi);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5*60*1000, pi);
}
}
启动接收器:
package com.my.app;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class StartUpReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, StartUpService.class);
context.startService(service);
}
}
启动服务:
package com.my.app;
import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.IBinder;
import android.os.Vibrator;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.widget.Toast;
public class StartUpService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
return;
}
@Override
public void onDestroy() {
return;
}
@Override
public void onStart(Intent intent, int startid) {
String preferences = "SERVICE";
SharedPreferences settings = getSharedPreferences(preferences, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstrun", false);
editor.commit();
CookieSyncManager.getInstance().sync();
CookieManager cookie_manager = CookieManager.getInstance();
String cookies = cookie_manager.getCookie("myurl");
if(cookies != null) {
int index = cookies.indexOf("sessiecode");
if(index != -1) {
String[] cookie = cookies.split("sessiecode=");
String c = cookie[cookie.length - 1];
new GetInteger().execute("myurl");
}
}
}
class GetInteger extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... uri) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
HttpGet post = new HttpGet(uri[0]);
response = httpclient.execute(post);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
String responseString = out.toString();
return responseString;
} else {
response.getEntity().getContent().close();
Log.e("com.my.app", "HTTPRequest error");
}
} catch (Exception e) {
Log.e("com.my.app", "Error: " + e.getMessage());
e.printStackTrace();
}
*/
return "5";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
int responseInt = Integer.parseInt(result);
int NOTIF_ID = 1234;
NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notifManager.cancel(NOTIF_ID);
if(responseInt > 0) {
Notification note = new Notification(R.drawable.ic_note, "Nieuw privébericht", System.currentTimeMillis());
PendingIntent pintent = PendingIntent.getActivity(One2xsService.this, 0, new Intent(StartUpService.this, MainActivity.class), 0);
String noteString;
if(responseInt > 1) {
noteString = "Je hebt " + responseInt + " ongelezen berichten";
} else {
noteString = "Je hebt 1 ongelezen bericht";
}
note.setLatestEventInfo(One2xsService.this, "Nieuw privébericht", noteString, pintent);
note.flags = Notification.FLAG_AUTO_CANCEL;
notifManager.notify(NOTIF_ID, note);
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(200);
}
}
}
}
我希望任何人都可以提供帮助!
编辑,记录:
02-02 19:00:27.100: E/AndroidRuntime(261): FATAL EXCEPTION: main
02-02 19:00:27.100: E/AndroidRuntime(261): java.lang.RuntimeException: Unable to start service com.my.app.StartUpService@40518b30 with Intent { cmp=com.my.app/.StartUpService }: java.lang.IllegalStateException: CookieSyncManager::createInstance() needs to be called before CookieSyncManager::getInstance()
02-02 19:00:27.100: E/AndroidRuntime(261): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2052)
02-02 19:00:27.100: E/AndroidRuntime(261): at android.app.ActivityThread.access$2800(ActivityThread.java:117)
02-02 19:00:27.100: E/AndroidRuntime(261): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:994)
02-02 19:00:27.100: E/AndroidRuntime(261): at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 19:00:27.100: E/AndroidRuntime(261): at android.os.Looper.loop(Looper.java:123)
02-02 19:00:27.100: E/AndroidRuntime(261): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-02 19:00:27.100: E/AndroidRuntime(261): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 19:00:27.100: E/AndroidRuntime(261): at java.lang.reflect.Method.invoke(Method.java:507)
02-02 19:00:27.100: E/AndroidRuntime(261): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-02 19:00:27.100: E/AndroidRuntime(261): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-02 19:00:27.100: E/AndroidRuntime(261): at dalvik.system.NativeStart.main(Native Method)
02-02 19:00:27.100: E/AndroidRuntime(261): Caused by: java.lang.IllegalStateException: CookieSyncManager::createInstance() needs to be called before CookieSyncManager::getInstance()
02-02 19:00:27.100: E/AndroidRuntime(261): at android.webkit.CookieSyncManager.getInstance(CookieSyncManager.java:81)
02-02 19:00:27.100: E/AndroidRuntime(261): at com.blay.one2xs.One2xsService.onStart(One2xsService.java:56)
02-02 19:00:27.100: E/AndroidRuntime(261): at android.app.Service.onStartCommand(Service.java:428)
02-02 19:00:27.100: E/AndroidRuntime(261): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2039)
02-02 19:00:27.100: E/AndroidRuntime(261): ... 10 more