为什么我的服务不开始?
问题描述:
我使用AlarmManager启动服务,但出于某种原因,我无法弄清楚服务未启动。为什么我的服务不开始?
在我的onResume()的代码用于启动该服务是:
Intent appIntent = new Intent(cxt, NotificationService.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent penIntent = PendingIntent.getService(this, 0,
appIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(penIntent);
penIntent = PendingIntent.getService(this, 0, appIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 5);
cal.set(Calendar.MINUTE, 00);
cal.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, cal.getTimeInMillis(),
10 * 1000, penIntent);
清单中包含以下内容:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:debuggable="true"
android:icon="@drawable/icon"
android:label="@string/app_name" >
<service
android:name="de.brillow.mensa.Speiseplan$NotificationService"
android:enabled="true" />
最后是服务本身是这样的:
public class NotificationService extends Service {
private WakeLock mWakeLock;
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void handleIntent(Intent intent) {
// obtain the wake lock
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"wakelocktag");
mWakeLock.acquire();
Log.i("PowerManager", "acquired");
// do the actual work, in a separate thread
new PollTask().execute();
}
private class PollTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// do stuff!
Log.i("Background", "stuff");
return null;
}
@Override
protected void onPostExecute(Void result) {
// handle your data
int id = 001;
Log.i("Noti", "fication");
NotificationManager mNotifyMng = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
Speiseplan.this).setSmallIcon(R.drawable.icon)
.setContentTitle("Test").setContentText("Test!");
mNotifyMng.notify(id, mBuilder.build());
Log.i("Notification", "sent");
stopSelf();
Log.i("Service", "stopped");
}
}
@Override
public void onStart(Intent intent, int startId) {
handleIntent(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleIntent(intent);
return START_NOT_STICKY;
}
public void onDestroy() {
super.onDestroy();
mWakeLock.release();
}
}
由于某种原因,通知和Log.i消息都不出现。我希望你能帮忙。
在此先感谢!
答
您不应在Service
中使用AsyncTask
。 AsyncTask
只应用于将结果返回到当前活动中的任务,即它应该在Activity
或Fragment
中定义。这就是它的设计目的。
对于您的情况,您可以将doInBackground(...)
和onPostExecute(...)
之间的所有内容放入您的服务的onCreate(...)
。如果你想使用线程。如果您需要在Activity
中显示服务结果,请调用Intent
并将任何详细信息转换为附加内容,例如,
调用服务...
Intent intent=new Intent(context,VisualizingActivity.class);
intent.putExtra("A", "Data A");
intent.putExtra("B", "Data B");
context.startActivity(intent);
处理的活动......
String A= intent.getStringExtra("A");
String B= intent.getStringExtra("B");
如果你只是想创建Notification
是你可以从服务直接做到。不需要AsyncTask
或Activity
。
希望这会有所帮助...干杯!
它有一点帮助,谢谢。但它不能解决我的问题。该服务(尽可能严格编码)dooesn甚至没有开始... – TheBrillowable 2013-04-04 19:56:49
1.删除'AsyncTask'; 2.在Manifest中声明服务:'android:name =“de.brillow.mensa.NotificationService”'; 3.从某处开始你的服务:'Intent service = new Intent(context,NotificationService.class); context.startService(service);' – Trinimon 2013-04-04 20:02:24
这是我犯的错误。我把服务放入我的主要活动.java文件中。把它放在一个新的文件中,并进行你提到的调整,现在开始,非常感谢! – TheBrillowable 2013-04-04 20:11:55