80,493
社区成员
发帖
与我相关
我的任务
分享
package com.service;
import com.biz.MutualBiz;
import com.bookread.MagaActivity;
import com.bookread.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
public class MagaService extends Service implements Runnable {
private int id;
private NotificationManager nfm;
@Override
public void run() {
MutualBiz biz = new MutualBiz(getApplicationContext());
try {
biz.saveyArtice(id);
} catch (Exception e) {
e.printStackTrace();
} finally {
onDestroy();
}
}
@Override
public void onStart(Intent intent, int startId) {
Bundle bundle = intent.getExtras();
id = bundle.getInt("magaId");
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
displayNotificationMessage("下载完成");
super.onDestroy();
}
@Override
public void onCreate() {
nfm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Thread thread = new Thread(this);
thread.start();
super.onCreate();
}
private void displayNotificationMessage(String message) {
Notification notification = new Notification(R.drawable.icon_down,
message, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MagaActivity.class), 0);
notification.setLatestEventInfo(this, "Background Service", message,
contentIntent);
nfm.notify(R.id.app_notification_id, notification);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}