gpt4 book ai didi

android - 我如何(应该)将处理程序添加到服务中的线程

转载 作者:行者123 更新时间:2023-11-29 02:12:39 27 4
gpt4 key购买 nike

我仍在研究 Pro Android 2 的简短服务示例(第 304 页)同样,服务示例由两个类组成:如下所示的 BackgroundService.java 和如下所示的 MainActivity.java。现在我想扩展此代码以将数据传递到我的应用程序中的另一个 Activity 。根据我所学到的,我在下面的代码中添加了一个处理程序的开头:

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

我了解到,当我获得服务实例时,我可以将其传递给处理程序。但我不知道该怎么做。

最佳答案

如果要从服务向 Activity 发送信息,请使用广播接收器。在您的 Activity 中创建一个广播接收器内部类,这样它就可以访问 Activity 的数据。然后注册广播:

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

然后从服务中,您可以通过 Intent 将数据发送到广播(和外部 Activity ):

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

关于android - 我如何(应该)将处理程序添加到服务中的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6320689/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com