CHANGED调用的onReceive

CHANGED调用的onReceive

本文介绍了BroadcastReceiver的为WALLPAPER_CHANGED调用的onReceive()多次:安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个BroadcastReceiver,并宣布它是这样的:

 <接收器
    机器人:名称=com.services.Receiver
    机器人:启用=真
    机器人:出口=真正的>
    <意图过滤器的Andr​​oid版本:优先=999>
        <作用机器人:名称=android.intent.action.WALLPAPER_CHANGED/>
    &所述; /意图滤光器>
< /接收器>
 

和接收器是:

  @覆盖
公共无效的onReceive(最终上下文的背景下,最终的意图意图)
{
    change_wallpepar.my preFS = context.getShared preferences(我的preFS,Context.MODE_PRIVATE);
    新的处理程序()。postDelayed(新的Runnable()
    {
        @覆盖
        公共无效的run()
        {
            Log.d(MAYUR,&其中;&所述; wallpepar改变>>中);
            如果(change_wallpepar.my prefs.getLong(temp_for_change,1)== 0)
            {
                context.stopService(新意图(背景下,change_wallpepar.class));
            }
            其他
            {
                共享preferences.Editor E = change_wallpepar.my prefs.edit();
                e.putLong(temp_for_change,0);
                e.commit();
            }
        }
    },4000);
}
 

在我这里改变墙纸应该叫一次。这是真正的工作由我的预期了一会儿,几分钟它会调用在的onReceive()多(10-18)倍,即使在壁纸换做一次。即使这个更奇怪的是,它是工作的罚款对三星Galaxy平板版本4.4.2,但不工作的摩托罗拉(MOTOē4.4.4)。

我的服务:

 公共类change_wallpepar延伸服务{

    @覆盖
    公共无效的onCreate()
    {
        // TODO自动生成方法存根
        super.onCreate();
        mytimer =新的Timer();
        WPM = WallpaperManager.getInstance(change_wallpepar.this);
    }

    @覆盖
    公众诠释onStartCommand(意向意图,诠释标志,INT startId)
    {
        // TODO自动生成方法存根
        我的preFS = getShared preferences(我的preFS,MODE_PRIVATE);
        INTERVALL =我的prefs.getLong(someValue中,60000);

        path_of_wallpepar.clear();
        path_of_wallpepar.add(+/storage/emulated/0/Android/data/WallpeparAppHistoryPhotos/514.jpg);
        path_of_wallpepar.add(+/storage/emulated/0/Android/data/WallpeparAppHistoryPhotos/513.jpg);

        DisplayImageOptions defaultOption =新DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
        ImageLoaderConfiguration配置=新ImageLoaderConfiguration.Builder(change_wallpepar.this).defaultDisplayImageOptions(defaultOption).build();
        。ImageLoader.getInstance()的init(配置);

        mytimer.schedule(新的TimerTask()
        {
            @覆盖
            公共无效的run()
            {

                尝试
                {
                    wpm.setBitmap(ImageLoader.getInstance()loadImageSync(文件://+ path_of_wallpepar.get(脾气)));

                }
                赶上(IOException异常E)
                {
                    e.printStackTrace();
                }

                脾气++;

                如果(脾气== path_of_wallpepar.size())
                    脾气= 0;

                共享preferences.Editor E = change_wallpepar.my prefs.edit();
                e.putLong(temp_for_change,1);
                e.commit();

                Log.e(MAYUR,wallpepar seted);

            }
        },0,INTERVALL);

        返回super.onStartCommand(意向,标志,startId);

    }

    @覆盖
    公众的IBinder onBind(意向意图)
    {
        // TODO自动生成方法存根

        返回null;
    }

    公共无效的onDestroy()
    {
        Toast.makeText(本,服务毁,Toast.LENGTH_SHORT).show();
        振动器V =(震动)getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(1000);
        mytimer.cancel();
        super.onDestroy();
    }

}
 

我的logcat的输出是:

  ## logcat的##
11月4号至16号:06:30.654:E / MAYUR(3405):wallpepar seted
11月4号至16号:06:34.636:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:06:59.584:E / MAYUR(3405):wallpepar seted
11月4号至16号:07:03.551:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:07:30.078:E / MAYUR(3405):wallpepar seted
11月4号至16号:07:33.979:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:07:59.433:E / MAYUR(3405):wallpepar seted
11月4号至16号:08:03.340:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:08:30.029:E / MAYUR(3405):wallpepar seted
11月4号至16号:08:33.933:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:08:59.481:E / MAYUR(3405):wallpepar seted
11月4号至16号:09:03.383:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:09:30.066:E / MAYUR(3405):wallpepar seted
11月4号至16号:09:33.966:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:09:59.448:E / MAYUR(3405):wallpepar seted
11月4号至16号:10:03.353:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:10:3​​0.049:E / MAYUR(3405):wallpepar seted
11月4号至16号:10:3​​3.955:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:10:59.455:E / MAYUR(3405):wallpepar seted
11月4号至16号:11:03.350:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:11:30.182:E / MAYUR(3405):wallpepar seted
11月4号至16号:11:34.177:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:11:59.406:E / MAYUR(3405):wallpepar seted
11月4号至16号:12:03.315:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:12:30.025:E / MAYUR(3405):wallpepar seted
11月4号至16号:12:33.929:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:12:34.103:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:12:34.298:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:12:34.497:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:12:34.676:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:12:34.854:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:12:35.022:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:12:35.190:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:12:35.355:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:12:35.522:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:12:35.683:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:12:35.852:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:12:36.023:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:12:36.187:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:12:36.350:D / MAYUR(3405):其中;&其中; wallpepar改变>>
11月4号至16号:12:36.513:D / MAYUR(3405):其中;&其中; wallpepar改变>>
 

解决方案

重复 WALLPAPER_CHANGED 呼叫通过在图像上运行的作物规模的周期,以适应较小的Andr​​oid设备引起的屏幕。这在 AOSP code 。你是不太可能看到此行为时,屏幕比例符合或大于形象,因此为什么平板电脑不会出现这种行为。

您可以解决这个问题,通过仔细检查了不受欢迎的行为的迹象:

 长lastExec = System.currentTimeMillis的();

@覆盖
公共无效的onReceive(最终上下文的背景下,最终的意图意图)
{
    change_wallpepar.my preFS = context.getShared preferences(我的preFS,Context.MODE_PRIVATE);
    新的处理程序()。postDelayed(新的Runnable()
    {
        @覆盖
        公共无效的run()
        {
          如果(System.currentTimeMillis的() -  lastExec> 1000)
          {
            Log.d(MAYUR,&其中;&所述; wallpepar改变>>中);
            如果(change_wallpepar.my prefs.getLong(temp_for_change,1)== 0)
            {
                context.stopService(新意图(背景下,change_wallpepar.class));
            }
            其他
            {
                共享preferences.Editor E = change_wallpepar.my prefs.edit();
                e.putLong(temp_for_change,0);
                e.commit();
            }
          }
          lastExec = System.currentTimeMillis的();
        }
    },4000);
}
 

I have a BroadcastReceiver and declared it like this:

<receiver
    android:name="com.services.Receiver"
    android:enabled="true"
    android:exported="true" >
    <intent-filter android:priority="999" >
        <action android:name="android.intent.action.WALLPAPER_CHANGED" />
    </intent-filter>
</receiver>

and the receiver is:

@Override
public void onReceive(final Context context, final Intent intent)
{
    change_wallpepar.myPrefs = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
    new Handler().postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
            Log.d("MAYUR", "<< wallpepar changed >>");
            if (change_wallpepar.myPrefs.getLong("temp_for_change", 1) == 0)
            {
                context.stopService(new Intent(context, change_wallpepar.class));
            }
            else
            {
                SharedPreferences.Editor e = change_wallpepar.myPrefs.edit();
                e.putLong("temp_for_change", 0);
                e.commit();
            }
        }
    }, 4000);
}

When I change the wallpaper here it should be called once. It is really working as by my expectations for a while, after some minutes it calls onreceive() multiple (10-18) times, even though the change in wallpaper is done once. Even more strange about this is that it is working fine on a Samsung Galaxy tablet version 4.4.2, but not working on Motorola (Moto E 4.4.4).

My service:

public class change_wallpepar extends Service {

    @Override
    public void onCreate()
    {
        // TODO Auto-generated method stub
        super.onCreate();
        mytimer = new Timer();
        wpm = WallpaperManager.getInstance(change_wallpepar.this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        // TODO Auto-generated method stub
        myPrefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
        intervall = myPrefs.getLong("someValue", 60000);

        path_of_wallpepar.clear();
        path_of_wallpepar.add("" + "/storage/emulated/0/Android/data/WallpeparAppHistoryPhotos/514.jpg");
        path_of_wallpepar.add("" + "/storage/emulated/0/Android/data/WallpeparAppHistoryPhotos/513.jpg");

        DisplayImageOptions defaultOption = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(change_wallpepar.this).defaultDisplayImageOptions(defaultOption).build();
        ImageLoader.getInstance().init(config);

        mytimer.schedule(new TimerTask()
        {
            @Override
            public void run()
            {

                try
                {
                    wpm.setBitmap(ImageLoader.getInstance().loadImageSync("file://" + path_of_wallpepar.get(temper)));

                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }

                temper++;

                if (temper == path_of_wallpepar.size())
                    temper = 0;

                SharedPreferences.Editor e = change_wallpepar.myPrefs.edit();
                e.putLong("temp_for_change", 1);
                e.commit();

                Log.e("MAYUR", "wallpepar seted");

            }
        }, 0, intervall);

        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public IBinder onBind(Intent intent)
    {
        // TODO Auto-generated method stub

        return null;
    }

    public void onDestroy()
    {
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(1000);
        mytimer.cancel();
        super.onDestroy();
    }

}

My Logcat output is:

## Logcat ##
04-16 11:06:30.654: E/MAYUR(3405): wallpepar seted
04-16 11:06:34.636: D/MAYUR(3405): << wallpepar changed >>
04-16 11:06:59.584: E/MAYUR(3405): wallpepar seted
04-16 11:07:03.551: D/MAYUR(3405): << wallpepar changed >>
04-16 11:07:30.078: E/MAYUR(3405): wallpepar seted
04-16 11:07:33.979: D/MAYUR(3405): << wallpepar changed >>
04-16 11:07:59.433: E/MAYUR(3405): wallpepar seted
04-16 11:08:03.340: D/MAYUR(3405): << wallpepar changed >>
04-16 11:08:30.029: E/MAYUR(3405): wallpepar seted
04-16 11:08:33.933: D/MAYUR(3405): << wallpepar changed >>
04-16 11:08:59.481: E/MAYUR(3405): wallpepar seted
04-16 11:09:03.383: D/MAYUR(3405): << wallpepar changed >>
04-16 11:09:30.066: E/MAYUR(3405): wallpepar seted
04-16 11:09:33.966: D/MAYUR(3405): << wallpepar changed >>
04-16 11:09:59.448: E/MAYUR(3405): wallpepar seted
04-16 11:10:03.353: D/MAYUR(3405): << wallpepar changed >>
04-16 11:10:30.049: E/MAYUR(3405): wallpepar seted
04-16 11:10:33.955: D/MAYUR(3405): << wallpepar changed >>
04-16 11:10:59.455: E/MAYUR(3405): wallpepar seted
04-16 11:11:03.350: D/MAYUR(3405): << wallpepar changed >>
04-16 11:11:30.182: E/MAYUR(3405): wallpepar seted
04-16 11:11:34.177: D/MAYUR(3405): << wallpepar changed >>
04-16 11:11:59.406: E/MAYUR(3405): wallpepar seted
04-16 11:12:03.315: D/MAYUR(3405): << wallpepar changed >>
04-16 11:12:30.025: E/MAYUR(3405): wallpepar seted
04-16 11:12:33.929: D/MAYUR(3405): << wallpepar changed >>
04-16 11:12:34.103: D/MAYUR(3405): << wallpepar changed >>
04-16 11:12:34.298: D/MAYUR(3405): << wallpepar changed >>
04-16 11:12:34.497: D/MAYUR(3405): << wallpepar changed >>
04-16 11:12:34.676: D/MAYUR(3405): << wallpepar changed >>
04-16 11:12:34.854: D/MAYUR(3405): << wallpepar changed >>
04-16 11:12:35.022: D/MAYUR(3405): << wallpepar changed >>
04-16 11:12:35.190: D/MAYUR(3405): << wallpepar changed >>
04-16 11:12:35.355: D/MAYUR(3405): << wallpepar changed >>
04-16 11:12:35.522: D/MAYUR(3405): << wallpepar changed >>
04-16 11:12:35.683: D/MAYUR(3405): << wallpepar changed >>
04-16 11:12:35.852: D/MAYUR(3405): << wallpepar changed >>
04-16 11:12:36.023: D/MAYUR(3405): << wallpepar changed >>
04-16 11:12:36.187: D/MAYUR(3405): << wallpepar changed >>
04-16 11:12:36.350: D/MAYUR(3405): << wallpepar changed >>
04-16 11:12:36.513: D/MAYUR(3405): << wallpepar changed >>
解决方案

The repeated WALLPAPER_CHANGED calls are caused by smaller Android devices running crop-scale cycles on the image to fit the screen. This is observed in the AOSP code. You're less likely to see this behavior when the screen ratio fits or is bigger than the image, hence why the tablet doesn't exhibit this behavior.

You can fix this problem by double-checking for signs of the unwanted behavior:

long lastExec = System.currentTimeMillis();

@Override
public void onReceive(final Context context, final Intent intent)
{
    change_wallpepar.myPrefs = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
    new Handler().postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
          if(System.currentTimeMillis()-lastExec>1000)
          {
            Log.d("MAYUR", "<< wallpepar changed >>");
            if (change_wallpepar.myPrefs.getLong("temp_for_change", 1) == 0)
            {
                context.stopService(new Intent(context, change_wallpepar.class));
            }
            else
            {
                SharedPreferences.Editor e = change_wallpepar.myPrefs.edit();
                e.putLong("temp_for_change", 0);
                e.commit();
            }
          }
          lastExec = System.currentTimeMillis();
        }
    }, 4000);
}

这篇关于BroadcastReceiver的为WALLPAPER_CHANGED调用的onReceive()多次:安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:20