English 中文(简体)
如何利用广播员更新两项活动?
原标题:How to use a BroadcastReceiver to update two activities?

我的申请仅涉及两项活动,即主要活动和优惠行动。 当各州出现变化时(航空模式正在转轨或关闭,全球定位系统被转用或关闭等),我想更新这两项活动。

我需要更新一些变数,而且只有当评估活跃时,才需要评估,因此我相信,最好在编码上而不是在申请表上登记接收器。

由于我有意在每项活动中重复守则,我尝试将广播接收器放在自己的班级。 然而,我发现我无法登记广播接收员。 这一问题是否有办法?

感谢您, 页: 1

public class melsBigListener {


IntentFilter intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE");
BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        //TO DO Update both my main activity and the preference activity
        //TO DO deal with different intents via switch statement... 
    } 

}; 

//registering as receiver(like below) can not work
//registerReceiver(receiver, intentFilter);}

Relevant post:RegisterBroadcastReceiver in Manifest.xmlring or by implementing source Code

问题回答
registerReceiver(receiver, intentFilter); should be in some method.

。 应扩大活动范围,或需要将活动环境纳入这一类别,并将其用于登记接收者。

最好不要做这种sil弄,而是在最坏的档案中写到接收器。

I suggest registering the receiver in the manifest and use it to alter variables stored in a preferences file (whether the app is running or not) and then query the preferences for state when needed.

But you could extend Application and register the receiver in code in the Application.onCreate() method.

BroadcastReceivers should work as an extended class that is registered to the phone using the manifest file. I m not too sure how it will work as an instantiated class inside an activity. judging by your code so far, the intent will never even be received by the Activity and your BigListener class because the intent filter was never declared in the manifest.

To solve your problem: 1. create a myBroadcastReceiver, extending from BroadcastReceiver, declaring the necessary intents in the manifest 2. create a myApplication, extending from Application 3. use your Activities onCreate, onDestroy, onStartCmd, onPause etc to manage the "only when the app is active" part, by toggling a variable in myApplication. 4. use myBroadcastReceiver to read off the variable in (3) whenever the intent is received, to decide if you "need to update some variables" and update accordingly. Since these variables are shared by your 2 Activities, the variables should be put inside the myApplication class





相关问题
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 ...

热门标签