我仍在研究Pro Android 2的简短服务示例(第304页)。同样,服务示例包含两个类:下面显示的BackgroundService.java和下面显示的MainActivity.java。现在我想扩展此代码以将数据传递给我的应用程序中的另一个活动。根据我的学习,我将处理程序的开头添加到下面的代码中:
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(TAG, "starting service");
Button bindBtn = (Button)findViewById(R.id.bindBtn);
bindBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent backgroundService = new Intent(MainActivity.this, com.marie.mainactivity.BackgroundService.class);
startService(backgroundService);
}
});
Button unbindBtn = (Button)findViewById(R.id.unbindBtn);
unbindBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
stopService(new Intent(MainActivity.this, BackgroundService.class));
}
});
}
}
// The handler code I added
// I'm not sure what fills out the msg.what field for the switch
private Handler messageHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
//handle update
//possibly update another activity???
}
}
};
public class BackgroundService extends Service {
private NotificationManager notificationMgr;
@Override
public void onCreate() {
super.onCreate();
notificationMgr = NotificationManager)getSystemService(NOTIFICATION_SERVICE);
displayNotificationMessage("starting Background Service");
Thread thr = new Thread(null, new ServiceWorker(), "BackgroundService");
thr.start();
}
class ServiceWorker implements Runnable
{
public void run() {
mResult = doSomethingTimeConsuming();
//Use the handler to send update to the main thread or another activity???
messageHandler.sendMessage(Message.obtain(messageHandler, mResults));
BackgroundService.this.stopSelf();
}
}
@Override
public void onDestroy()
{
displayNotificationMessage("stopping Background Service");
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void displayNotificationMessage(String message)
{
Notification notification = new Notification(R.drawable.note, message, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
notification.setLatestEventInfo(this, "Background Service", message, contentIntent);
notificationMgr.notify(R.id.app_notification_id, notification);
}
}
我已经了解到,当我获得Service实例时,我可以将其传递给Handler。但我不知道该怎么做。
答案 0 :(得分:1)
如果要将信息从服务发送到活动,请使用广播接收器。制作一类您的活动的广播接收器内部类,这样它就可以访问活动数据。然后注册广播:
IntentFilter filter = new IntentFilter("com.damluar.intent.action.NEWTWEETS");
broadcastReceiver = new NewTweetsReceiver();
registerReceiver(broadcastReceiver, filter);
然后从服务中你可以通过意图将数据发送到广播(和外部活动):
sendBroadcast(new Intent("com.damluar.intent.action.NEWTWEETS"));
答案 1 :(得分:0)
您可以在setHandler(Handler handler)
类中定义BackgroundSerivce
setter函数,并在启动服务之前从MainActivity
调用它并以这种方式传递处理程序。