English 中文(简体)
正在从操作器内和机器人的操作员处获取应用程序实例
原标题:getting Application instance from a handler in android

I am trying to solve the following problem: I am writing an application that has multiple activities that share data model. The data is fetched from DB when application starts and saved as global variables in Application class extension as follow:

class MyApp extends Application {

  private MyData myData;

  public MyData getData(){
    return myData;
  }
  public void setData(MyData d){
    myData = d;
    sendBroadcast(new Intent("DATA_UPDATED"););
  }
}

The AndroidManifest.xml is updated of course and everything works great - every activity can read the data and update it, other activities can get notifications of data change using the BroadcasrReceiver. Things get problematic when I have another thread that should update the main (GUI) thread: I have a service that contains a callback when new data is received from the db. the callback is running on new thread, so updating my Apllication data model must being done on the main thread. for that I used handler as follow:

public void ServiceCallback(...newData) {           
    //Pass the message up to our handler to make the update on the main thread.
    Message receipt = Message.obtain(mHandler, 0, newData);
    receipt.sendToTarget();       
}

    //Handle incoming message from remote on the main thread (GUI thread)
    private Handler mHandler = new Handler() 
{
     @Override
     public void handleMessage(Message msg) 
     {
    //read new data from the message - from msg.obj field, no prob.             
    //but - how can i get to my application model instance????
     }
};

但我看到「https://stackoverflow. com/ questions/4481928和roid-getting-activity-instance- in- in-application- from- handler」,

我不知道它是否真实,为什么,也许我还有另一个解决办法,谁可以建议呢?我真的很感激。能否在线间使用广播接收器?也许这就是我的解决方法?

问题回答

有点不清楚您到底要做什么。 需要注意的有几件事: 使用一个共享/ 全球数据应用程序对象并不能真正为您买到任何东西。 您可以简单地使用一个单吨。 如果您想要使用 < code> Application , 您可以在它添加一个静态 < code> getInstance () 方法, 因为只有一个例子是单吨, 添加该方法只会让您更容易从非活动类获取数据。 您不需要在主线上更新 < code> Application 类中的数据。 您在处理 UI 时只需要在主线上做一些事情 。

此外,如果您只想将信件发送到另一个线索, Handler 就足够了。不需要包含 BroadcastReceivers





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签