从服务更新 Activity 中的文本视图标题
update textview title in an Activity from a service
我有一个 homeActivity,它包含一个带有 actionbar 的 webview,actionbar 的标题是一个 textview
public static TextView mTitleTextView;
并且还有一个 class 接收 gcm 消息
public class GCMNotificationIntentService extends IntentService {
应用程序收到一条消息后,我想将字符串放入 homeActivity 的文本视图中,我尝试使用
HomeActivity.mTitleTextView.setText("9999999999999999999999999999999999999999999999999999999999");
但是应用程序因错误而关闭,我读过一些旧的 post 并用谷歌搜索看到广播接收器之类的东西可以解决这个问题,但我不太明白它是如何工作的,任何人都可以显示一些实际来源可以应用在我的情况下的代码?
使用处理程序并从 Intentservice
向父 activity 发送消息
父级 Activity :
正在声明处理程序
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Bundle reply = msg.getData();
// do whatever with the bundle here
}
};
调用意图服务:
Intent intent = new Intent(this, IntentService1.class);
intent.putExtra("messenger", new Messenger(handler));
startService(intent);
内部 IntentService:
Bundle bundle = intent.getExtras();
if (bundle != null) {
Messenger messenger = (Messenger) bundle.get("messenger");
Message msg = Message.obtain();
msg.setData(data); //put the data here
try {
messenger.send(msg);
} catch (RemoteException e) {
Log.i("error", "error");
}
}
或
如果你想使用 BroadcastReceiver
here 是很好的例子。
我们可以通过使用handler,broadcat和Listener来实现concept.But我觉得broadcast很容易实现和理解但是需要注意广播的注册和注销
使用侦听器
创建监听器class
public Interface Listener{
public void onResultReceived(String str);
}
现在在 activity 中实现它,如下所示
public class MainActivity extends Activity implements listener{
public void onResultReceived(String str){
mTitleTextView.setText(str)
}
}
通过从 Activity
的 oncreate 调用服务的构造函数来初始化您的监听器
new GCMNotificationIntentService (MainActivity.this);
现在为您的服务创建 public 构造函数,如下所示
public class GCMNotificationIntentService extends IntentService {
public static Listener listener_obj;
public GCMNotificationIntentService (Listener listener)
{
listener_obj=listener;
}
Listener.onResultReceived("99999999999999999999999999999999999999999");
//send the data which should be shown on textview
正在使用广播
registerReceiver( mMessageReceiver, new IntentFilter("GETDATA"));
//register localbraodcast with receiver object and intent filter inside oncreate
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String str= intent.getStringExtra("DATA", "No Data");
mTitleTextView.setText(str);
}
};
ondestroy()
{
unregisterReceiver( mMessageReceiver);
}
从服务发送数据
Intent intent = new Intent("GETDATA");
intent.putExtra("DATA", "9999999");
sendBroadcast(intent)
我有一个 homeActivity,它包含一个带有 actionbar 的 webview,actionbar 的标题是一个 textview
public static TextView mTitleTextView;
并且还有一个 class 接收 gcm 消息
public class GCMNotificationIntentService extends IntentService {
应用程序收到一条消息后,我想将字符串放入 homeActivity 的文本视图中,我尝试使用
HomeActivity.mTitleTextView.setText("9999999999999999999999999999999999999999999999999999999999");
但是应用程序因错误而关闭,我读过一些旧的 post 并用谷歌搜索看到广播接收器之类的东西可以解决这个问题,但我不太明白它是如何工作的,任何人都可以显示一些实际来源可以应用在我的情况下的代码?
使用处理程序并从 Intentservice
父级 Activity :
正在声明处理程序
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Bundle reply = msg.getData();
// do whatever with the bundle here
}
};
调用意图服务:
Intent intent = new Intent(this, IntentService1.class);
intent.putExtra("messenger", new Messenger(handler));
startService(intent);
内部 IntentService:
Bundle bundle = intent.getExtras();
if (bundle != null) {
Messenger messenger = (Messenger) bundle.get("messenger");
Message msg = Message.obtain();
msg.setData(data); //put the data here
try {
messenger.send(msg);
} catch (RemoteException e) {
Log.i("error", "error");
}
}
或
如果你想使用 BroadcastReceiver
here 是很好的例子。
我们可以通过使用handler,broadcat和Listener来实现concept.But我觉得broadcast很容易实现和理解但是需要注意广播的注册和注销
使用侦听器
创建监听器class
public Interface Listener{
public void onResultReceived(String str);
}
现在在 activity 中实现它,如下所示
public class MainActivity extends Activity implements listener{
public void onResultReceived(String str){
mTitleTextView.setText(str)
}
}
通过从 Activity
的 oncreate 调用服务的构造函数来初始化您的监听器new GCMNotificationIntentService (MainActivity.this);
现在为您的服务创建 public 构造函数,如下所示
public class GCMNotificationIntentService extends IntentService {
public static Listener listener_obj;
public GCMNotificationIntentService (Listener listener)
{
listener_obj=listener;
}
Listener.onResultReceived("99999999999999999999999999999999999999999");
//send the data which should be shown on textview
正在使用广播
registerReceiver( mMessageReceiver, new IntentFilter("GETDATA"));
//register localbraodcast with receiver object and intent filter inside oncreate
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String str= intent.getStringExtra("DATA", "No Data");
mTitleTextView.setText(str);
}
};
ondestroy()
{
unregisterReceiver( mMessageReceiver);
}
从服务发送数据
Intent intent = new Intent("GETDATA");
intent.putExtra("DATA", "9999999");
sendBroadcast(intent)