有关service的问题

a973261951aa 2012-06-08 01:33:52
Activity.startservice()方式调用service,service接收一个参数,根据这个参数去服务器上读取一些数据下来,在oncreate()中开启一个线程,但需要用到那个参数,但是参数好像不能再oncreate()中接收,新手,大家帮帮忙呗。

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;
}

}

...全文
122 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
balmy 2012-06-09
  • 打赏
  • 举报
回复
Serivce里面 重载下onStartCommand()这个方法 可以试试,不知道能不能解决问题
@Override
public int onStartCommand(Intent intent, int flags, int startId)
深夜深几许 2012-06-08
  • 打赏
  • 举报
回复
没有看出有什么错误的

80,493

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧