什么是创造的BroadcastReceiver的最佳方法是什么?方法、BroadcastReceiver

2023-09-12 05:19:58 作者:你使美人计我就将计就计

我花了几天工作在服务+ BroadcastReceiver的,但仍然不能做到尽善尽美。我希望有人能帮助,谢谢!

我写一个应用程序,在地图上显示用户的当前位置(即我写的地图,而不是谷歌地图),并发出提示报警,当用户进入一个predefined区域内。

在我的code。有两个主要的对象。的GPS服务和主要活动。

在位置改变的GPS服务广播的位置。主要活动通过的BroadcastReceiver收到最新的位置。

我已经做了怎样注册的BroadcastReceiver一些研究。还有,我发现有两种方式:

方法1 - 注册广播接收器的内部活动(我用这个方法在我的code我需要更新最新的地图位置)

Main.java

 公共类主要扩展活动{
。
。
。
    公共类MyLocReceiver扩展的BroadcastReceiver {

        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){
            // TODO自动生成方法存根

            捆绑包= intent.getExtras();
            串locData = bundle.getString(LOC);

            // 一些工作


        }
        公共MyLocReceiver(){
            (在MyReceiver()的标签,)Log.e;
        }

    }

    私人MyLocReceiver myLocReceiver;

    //注册的BroadcastReceiver
    如果(myLocReceiver == NULL)
        {
            Common.writeFile(service.txt,主 - 在myBindService()寄存器接收器+\ N,真正的);

            myLocReceiver =新MyLocReceiver();

            registerReceiver(myLocReceiver,新的IntentFilter(com.nwfb.LOC_DATA));
        }
 
Android开发之BroadcastReceiver和Service实例

方法2 - 注册的BroadcastReceiver在的Manifest.xml

的Manifest.xml:

 <接收器安卓名=机器人LocationBoardcastReceiver。:启用=真正的>
            <意向滤光器>
                <作用机器人:名称=com.abc.LOC_DATA/>
            &所述; /意图滤光器>
    < /接收器>
 

LocationBoardcastReceiver.java:

 公共类LocationBoardcastReceiver扩展的BroadcastReceiver {
        私有静态最后字符串变量= LocationBoardcastReceiver.class.getSimpleName();

        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){

        捆绑包= intent.getExtras();
        // 做一点事
        }
    }
 

* 的我想是的BroadcastReceiver绝不能在应用程序的生命时间操作系统杀害。另外,BroadcastReceiver的必须能够将数据传递给我的Main.java

我读到这actical: http://developer.android.com/reference/android/content/BroadcastReceiver.html

接收器生命周期和流程生命周期的状态,该的BroadcastReceiver将完成并不再有效后的onReceive(上下文,意图)调用的部分。

我使用的方法1,我可以从服务获得超过1地点的数据。我发现的BroadcastReceiver保持活着,如果的BroadcastReceiver保持从广播接收数据。 如果我关闭GPS在设置 - >位置,并让BroadcastReceiver的闲置约1至2小时,然后将的BroadcastReceiver打死OS

是否操作系统杀的BroadcastReceiver如果闲置时间过长?请问OS不杀的BroadcastReceiver,如果它不断接收广播?

请问BroadcastReceiver的(方法1和2)杀害操作系统时,它是在极端的内存pressure情况?

有关方法2,是有可能的LocationBoardcastReceiver.java将数据发送到运行的活动?(例如:主,JAVA)

有关方法1,是没有办法在Main.java(主要活动)的续航时间,以保持的BroadcastReceiver活着?

解决方案   

是否操作系统杀的BroadcastReceiver如果闲置时间过长?是否操作系统   不杀的BroadcastReceiver,如果它不断接收广播?

     

请问BroadcastReceiver的(方法1和2)杀害操作系统时,它是   在极端的内存pressure情况?

您接收进来,当它接收到的任何通知,行动和它的活动时间是它的的onReceive()方法。

  

有关方法2,是有可能的LocationBoardcastReceiver.java   将数据发送到正在运行的活动?(例如:主,JAVA)

坏主意。

  

有关方法1,是没有办法在保持BroadcastReceiver的活着   在Main.java(主要活动)?

的续航时间

检查上面。

您最好的办法将取决于在你的应用程序,它要求。

如果您的应用程序要使用的BroadcastReceiver,而应用程序是在前台,那么你应该去为方法1 正如你所提到的。

如果您的应用程序需要接收系统的通知,如启动完成等等,那么你应该去为方法2 正如你所提到的。

I have spent few days to work on Service + BroadcastReceiver, but still cannot make it perfect. I hope someone can help, thanks!

I am writing a App that show user current location on map(The map that I wrote, not Google Map) and send out Notification alarm when user go inside a predefined zone.

In my code. there are two main objects. A GPS service and Main Activity.

The GPS service broadcast location when location changed. The Main Activity receive the latest location by BroadcastReceiver.

I have done some researches on how to register BroadcastReceiver. There are two ways that I found:

Method 1 - Register Broadcast Receiver inside Activity (I am using this method in my code. I need to update latest location on map)

Main.java:

public class Main extends Activity{
.
.
.
    public class MyLocReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

            Bundle bundle=intent.getExtras();
            String locData = bundle.getString("loc");

            // Some work


        }
        public MyLocReceiver(){
            Log.e(TAG, "in MyReceiver()");
        }

    }

    private MyLocReceiver myLocReceiver;

    // Register BroadcastReceiver
    if (myLocReceiver == null)
        {
            Common.writeFile("service.txt", "Main - in myBindService() register receiver" + "\n", true);

            myLocReceiver=new MyLocReceiver();

            registerReceiver(myLocReceiver, new IntentFilter("com.nwfb.LOC_DATA"));
        }

Method 2 - Register BroadcastReceiver at Manifest.xml

Manifest.xml:

    <receiver android:name=".LocationBoardcastReceiver" android:enabled="true"> 
            <intent-filter>
                <action android:name="com.abc.LOC_DATA" /> 
            </intent-filter> 
    </receiver>

LocationBoardcastReceiver.java:

    public class LocationBoardcastReceiver extends BroadcastReceiver {
        private static final String TAG = LocationBoardcastReceiver.class.getSimpleName();

        @Override
        public void onReceive(Context context, Intent intent) {

        Bundle bundle=intent.getExtras();
        // Do something
        }
    }

* What I want is the BroadcastReceiver MUST NOT KILLED by the OS in the application life time. Also the BroadcastReceiver must able to pass data to my Main.java

I read this actical: http://developer.android.com/reference/android/content/BroadcastReceiver.html

The section of 'Receiver Lifecycle' and 'Process Lifecycle' state that the BroadcastReceiver will be finished and no longer active after onReceive(Context, Intent) called.

I am using Method 1, I can receive more than 1 Location data from the Service. I found that the BroadcastReceiver keeps alive if the BroadcastReceiver keeps receiving data from the Broadcast. If I turn off GPS at 'Setting -> Location ' and let the BroadcastReceiver idle for about 1 to 2 hours, then the BroadcastReceiver will killed by OS.

Does the OS kill BroadcastReceiver if it idle too long? Does the OS NOT kill the BroadcastReceiver if it keeps receiving broadcast?

Will the BroadcastReceiver (Method 1 and 2) killed by OS when it is under cases of extreme memory pressure?

For Method 2, is it possible that the LocationBoardcastReceiver.java send data to a running activity (Eg: Main,java)?

For Method 1, is it any way to keep the BroadcastReceiver alive during the life time of the Main.java (Main Activity)?

解决方案

Does the OS kill BroadcastReceiver if it idle too long? Does the OS NOT kill the BroadcastReceiver if it keeps receiving broadcast?

Will the BroadcastReceiver (Method 1 and 2) killed by OS when it is under cases of extreme memory pressure?

Your receiver comes in action when it receives any notification and it's active duration is it's onReceive() Method.

For Method 2, is it possible that the LocationBoardcastReceiver.java send data to a running activity (Eg: Main,java)?

Bad idea.

For Method 1, is it any way to keep the BroadcastReceiver alive during the life time of the Main.java (Main Activity)?

Check above.

Your best way will depend over your app what it requires.

If your app wants to use BroadcastReceiver while app is in foreground then you should go for Method1 as you have mentioned.

If your app need to receive system's notification as Boot Completion etc then you should go for Method2 as you have mentioned.