Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从服务更新活动中的文本视图标题_Android_Service_Textview - Fatal编程技术网

Android 从服务更新活动中的文本视图标题

Android 从服务更新活动中的文本视图标题,android,service,textview,Android,Service,Textview,我有一个homeActivity,它包含一个带有actionbar的webview,actionbar的标题是textview public static TextView mTitleTextView; 还有一个类可以接收gcm消息 public class GCMNotificationIntentService extends IntentService { 在应用程序收到一条我想将字符串放入homeActivity文本视图的消息后,我尝试使用 HomeActivity.mTitleT

我有一个homeActivity,它包含一个带有actionbar的webview,actionbar的标题是textview

public static TextView mTitleTextView;
还有一个类可以接收gcm消息

public class GCMNotificationIntentService extends IntentService {
在应用程序收到一条我想将字符串放入homeActivity文本视图的消息后,我尝试使用

HomeActivity.mTitleTextView.setText("9999999999999999999999999999999999999999999999999999999999");

但应用程序因错误而关闭,我读了一些旧帖子,在谷歌上看到类似广播接收器的东西可以解决这个问题,但我不太明白它是如何工作的,有人能展示一些可以在我的情况下应用的实际源代码吗?

使用处理程序并从
Intentservice

家长活动

声明处理程序

Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
            Bundle reply = msg.getData();
                // do whatever with the bundle here
            }
};
调用intentservice:

    Intent intent = new Intent(this, IntentService1.class);
    intent.putExtra("messenger", new Messenger(handler));
    startService(intent);
Inside 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");
    }
}


如果您想使用
广播接收器
就是一个很好的例子。

我们可以通过使用handler、broadcat和Listener概念来实现。但我认为广播很容易实现和理解,但需要注意广播的注册和注销

使用侦听器

创建一个侦听器类

public Interface Listener{
public void onResultReceived(String str);
}
现在在下面的活动中实现它

public class MainActivity extends Activity implements listener{
public void onResultReceived(String str){
mTitleTextView.setText(str)
 }
}
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
通过从活动的oncreate调用服务的构造函数初始化侦听器

new  GCMNotificationIntentService (MainActivity.this);
现在创建服务的公共构造函数,如下所示

public class MainActivity extends Activity implements listener{
public void onResultReceived(String str){
mTitleTextView.setText(str)
 }
}
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=新的BroadcastReceiver(){

从服务发送数据

Intent intent = new Intent("GETDATA");
intent.putExtra("DATA", "9999999");
sendBroadcast(intent)

您是否尝试过
HomeActivity h=new HomeActivity();
h.mTitleTextView.setText(“99999999”)
并且不要忘记将
mTitleTextView
公开但是当您想在前面的
主页活动中更新文本时,您可以在此处使用LocalBroadcastManager:@NealAhluvalia,您应该自己创建一个活动实例最好的方法是使用LocalBroadcastManagerst很容易实现和理解,但需要注意或注册和注销广播。因此,更好的人应该实现处理程序。是的,你是对的,但如果服务在后台连续运行,那么广播接收器是更好的选择Hi kartheek我认为你不能在put extras中发送messenger对象,它没有t支持您可以通过intent发送Messenger,因为它实现了Parcelable。您可以通过此链接我的IntentService类已经具有公共GCMNotificationtentService(){super(“GCMinentService”);},我将其更改为公共GCMNotificationtentService(Listener Listener){super(“GCMinentService”);Listener\u obj=Listener;},应用程序在启动时立即关闭,但不创建另一个单独的构造函数并将其添加到服务中。此构造函数的目的只是初始化侦听器。除此之外,我如何在不使用构造函数的情况下初始化侦听器?但GCMNotificationtentService不允许我添加公共意图服务(侦听器侦听器){listener_obj=listener;}让我们来。