2011-06-12 224 views
2

我仍在处理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); 
     } 
    } 

我已经学会了,当我得到的服务实例,我可以通过它的处理程序。但我不知道该怎么做。

回答

1

如果要将服务中的信息发送到活动,请使用广播接收器。为您的活动制作一类广播接收器内部类,以便它可以访问活动数据。通过意向

IntentFilter filter = new IntentFilter("com.damluar.intent.action.NEWTWEETS"); 
broadcastReceiver = new NewTweetsReceiver(); 
registerReceiver(broadcastReceiver, filter); 

然后从服务,您可以将数据发送到广播(和外活动):然后注册广播

sendBroadcast(new Intent("com.damluar.intent.action.NEWTWEETS")); 
+0

有趣的想法。之前没有听说过。它如何比较性能/记忆方面与更常见的服务解决方案(我不知道该怎么做)?也就是说,该服务拥有后台线程,并将服务实例传递给Handler。然后服务中的线程可以访问处理程序,并通过处理程序将消息发送到活动。 – Marie 2011-06-12 16:59:11

+0

@玛丽,我从来没有听说过你的方法(但这并不意味着它是坏的)。对我而言,我似乎更加轻松自然。对于实现,你也可以看到这个讨论:http://stackoverflow.com/questions/1464853/sending-data-from-service-to-activity – damluar 2011-06-12 17:14:43

+0

这很有趣。我所阅读的所有书籍和我所做的网络搜索都无法停止将服务作为解决方案。我厌倦了它,主要是因为我无法让它工作。我即将放弃。但也许广播接收机是我需要的东西。谢谢。 – Marie 2011-06-12 23:02:48

0

你可以定义一个setHandler(Handler handler) setter函数在BackgroundSerivce类并调用它在启动服务之前从您的MainActivity开始,并以此方式传递处理程序。

+0

做到这一点需要对我的BackgroundService的引用。 – Marie 2011-06-13 03:03:45

相关问题