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;
}}
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;
}}
解决方案 »
- 紧急求救:android刷机的问题
- 桌面控件的按钮事件怎样监听
- 再问谷歌地图的离线保存/读取
- 我想实现activity的缩放动画,但是...
- 关于 安卓模拟器跟电脑通讯的问题??
- 不能使用listview控件 方法里 getView() 里面 View 缓存。。。
- RelativeLayout中两个控件怎么居中显示
- 请教:开发普通网络app(非游戏),熟悉c#,该选cocos2d还是unity3d?
- 如何在自定义View的onLayout生命周期addView
- 求救,android netty的问题 java.io.IOException: Software caused connection abort
- 关于JS与android交互的问题
- 服务器端向android客户端传送List<Album>对象,客户端读不出来此对象
@Override
public int onStartCommand(Intent intent, int flags, int startId)