如何解决我应该如何向Service的线程中添加处理程序
|| 我仍在研究Pro Android 2的简短Service示例(第304页),该Service示例又由两个类组成:下面显示的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,new Intent(this,MainActivity.class),0);
notification.setLatestEventInfo(this,\"Background Service\",contentIntent);
notificationMgr.notify(R.id.app_notification_id,notification);
}
}
我了解到,当我获得Service实例时,可以将其传递给Handler。但是我不知道该怎么做。
解决方法
如果要将信息从服务发送到活动,请使用广播接收器。将广播接收器作为您的活动的内部类,这样它就可以访问活动的数据。然后注册广播:
IntentFilter filter = new IntentFilter(\"com.damluar.intent.action.NEWTWEETS\");
broadcastReceiver = new NewTweetsReceiver();
registerReceiver(broadcastReceiver,filter);
然后从服务中,您可以通过意图将数据发送到广播(和外部活动):
sendBroadcast(new Intent(\"com.damluar.intent.action.NEWTWEETS\"));
, 您可以在BackgroundSerivce
类中定义setHandler(Handler handler)
setter函数,并在启动服务之前从MainActivity
中调用它,并以这种方式传递处理程序。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。