对于我正在制作的Android应用程序,我希望有一个服务即使在应用程序没有运行时检查数据库是否有攻击(有点像facebook会通知某些内容,即使它是'我完成了下面的课程,但是在我停止运行我的应用程序后一分钟我得到了一条消息" App已经无法停止"然后几分钟后......我知道有可能继续检查,因为许多应用程序都这样做,但我做错了什么? 这就是我到目前为止所做的:
package com.ducknoise.toonfight;
import java.sql.SQLException;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.widget.Toast;
public class ToonService extends Service{
public static ToonService toonService;
public ToonService(){
toonService = this;
}
@Override
public IBinder onBind(Intent intent) {
String toonName = intent.getStringExtra("toonName");
ToonDB task = new ToonDB();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,toonName);
} else {
task.execute(toonName);
}
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String toonName = intent.getStringExtra("toonName");
ToonDB task = new ToonDB();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,toonName);
} else {
task.execute(toonName);
}
return Service.START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
Toast.makeText(getApplicationContext(), "Disconnected with Server", Toast.LENGTH_LONG).show();
Database db = new Database();
db.closeConnection();
super.onDestroy();
}
public class ToonDB extends AsyncTask<String,Void, Void>{
Toon toon;
@Override
protected Void doInBackground(String... params) {
Database db = new Database();
db.establishConnection();
toon = db.getToonFromDB(params[0]);
return null;
}
@Override
protected void onPostExecute(Void result) {
ToonCheck task = new ToonCheck();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,toon);
} else {
task.execute(toon);
}
super.onPostExecute(result);
}
}
public class ToonCheck extends AsyncTask<Toon,Void, Void>{
boolean runCheck;
Toon toon;
@Override
protected Void doInBackground(Toon... params) {
runCheck = true;
Database db = new Database();
db.establishConnection();
toon = params[0];
toon.updateCheckDB();
publishProgress();
if(!db.isOnline(toon))
runCheck = false;
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
Battle battle = toon.getBattle();
if(battle != null ){
if(battle.notified()){
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(getApplicationContext());
nBuilder.setContentTitle("Toon Fight");
String contentText = battle.getOppName() + " has attacked you!";
nBuilder.setContentText(contentText);
nBuilder.setSmallIcon(R.drawable.ic_launcher);
int notiID = 1295;
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(notiID, nBuilder.build());
}
}
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void result) {
if(runCheck){
ToonCheck task = new ToonCheck();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,toon);
} else {
task.execute(toon);
}
}else
stopSelf();
super.onPostExecute(result);
}
}
}
这是logcat
03-24 11:01:41.968: D/AndroidRuntime(4624): Shutting down VM
03-24 11:01:41.968: W/dalvikvm(4624): threadid=1: thread exiting with uncaught exception (group=0x41f5aac8)
03-24 11:01:41.968: E/AndroidRuntime(4624): FATAL EXCEPTION: main
03-24 11:01:41.968: E/AndroidRuntime(4624): java.lang.RuntimeException: Unable to start service com.ducknoise.toonfight.ToonService@430a3950 with null: java.lang.NullPointerException
03-24 11:01:41.968: E/AndroidRuntime(4624): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2782)
03-24 11:01:41.968: E/AndroidRuntime(4624): at android.app.ActivityThread.access$2000(ActivityThread.java:152)
03-24 11:01:41.968: E/AndroidRuntime(4624): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1385)
03-24 11:01:41.968: E/AndroidRuntime(4624): at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 11:01:41.968: E/AndroidRuntime(4624): at android.os.Looper.loop(Looper.java:137)
03-24 11:01:41.968: E/AndroidRuntime(4624): at android.app.ActivityThread.main(ActivityThread.java:5329)
03-24 11:01:41.968: E/AndroidRuntime(4624): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 11:01:41.968: E/AndroidRuntime(4624): at java.lang.reflect.Method.invoke(Method.java:511)
03-24 11:01:41.968: E/AndroidRuntime(4624): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
03-24 11:01:41.968: E/AndroidRuntime(4624): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
03-24 11:01:41.968: E/AndroidRuntime(4624): at dalvik.system.NativeStart.main(Native Method)
03-24 11:01:41.968: E/AndroidRuntime(4624): Caused by: java.lang.NullPointerException
03-24 11:01:41.968: E/AndroidRuntime(4624): at com.ducknoise.toonfight.ToonService.onStartCommand(ToonService.java:34)
03-24 11:01:41.968: E/AndroidRuntime(4624): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2765)
03-24 11:01:41.968: E/AndroidRuntime(4624): ... 10 more
这就是我调用该服务的方式:
Intent toonService = new Intent(MainActivity.this, ToonService.class);
toonService.putExtra("toonName", toon.getName());
MainActivity.this.startService(toonService);
答案 0 :(得分:0)
您应该根据intent
检查onStartCommand
null
logcat
是否为03-24 11:01:41.968: E/AndroidRuntime(4624): Caused by: java.lang.NullPointerException
03-24 11:01:41.968: E/AndroidRuntime(4624): at
com.ducknoise.toonfight.ToonService.onStartCommand(ToonService.java:34)
:
Service
请说明如何开始{{1}}