在BroadcastReceiver中更改ImageView图片

时间:2015-11-19 15:32:32

标签: java android service broadcastreceiver alarmmanager

快速问题,因为我自己无法弄明白。

首先,我不想作为乞讨出来,所以我会告诉你我所知道的。

这段代码效果很好:

package com.edip.splashwallpaper;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.WallpaperManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.UUID;

public class MainActivity extends AppCompatActivity {

    ImageView mImageView;
    Switch mSwitch;
    Bitmap bitmap;

    public class AlarmReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

//                getImage();
                Toast.makeText(MainActivity.this, "AlarmReceiver Works", Toast.LENGTH_SHORT).show();

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mImageView = (ImageView) findViewById(R.id.imageView);
        mSwitch = (Switch) findViewById(R.id.switch1);

        setAlarmManager(); //TODO zbog ovog mi se gasi AlarmManager. FIX IT!

    }

    private void setAlarmManager(){
        AlarmManager alrm = (AlarmManager) getSystemService(ALARM_SERVICE);
        final String IF_ACTION = "com.edip.splashwallpaper.AlarmReceiver";
        IntentFilter intentFilter = new IntentFilter(IF_ACTION);
        AlarmReceiver mReceiver = new AlarmReceiver();
        registerReceiver(mReceiver, intentFilter);
        Intent i2 = new Intent(IF_ACTION);
        PendingIntent pi = PendingIntent.getBroadcast(this, 2, i2, 0);
        alrm.setRepeating(AlarmManager.RTC, 5000, 60000, pi);
        Toast.makeText(this, "Alarm Started", Toast.LENGTH_SHORT).show();
    }

    public void getImage() {

        String mRandomKey = UUID.randomUUID().toString(); //Generates random string

        Picasso.with(MainActivity.this)
                .load("https://source.unsplash.com/category/nature/1920x1080/")
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.error_placeholder)
                .stableKey(mRandomKey) //new key is new picture
                .into(mImageView);

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
                setAsWallpaper(bitmap);
            }
        }, 5000); //adding 5 sec delay

    }

    public void setAsWallpaper(Bitmap bitmap) {
        try {
            WallpaperManager wm = WallpaperManager.getInstance(MainActivity.this);

            wm.setBitmap(bitmap);
            Toast.makeText(this, "New Wallpaper Set", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "Failed to set Wallpaper", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

代码正在运行且没有错误,每XY分钟都会更改背景图像。 问题BroadcastReceiver类位于MainActivity类内,如果用户清除“最近的应用”(或任何其他类型的内存清除,即CleanMaster) - AlarmManager停止

现在,我尝试单独制作BroadcastReceiver课程,并使用Toast对其进行测试,并且工作正常 - AlarmManager未停止。 问题,从NullPointerException调用 getImage() 函数时,我得到MainActivity。我的猜测是,BroadcastReceiver无法更新另一个ImageView的{​​{1}}。

我还尝试将Activity功能放在getImage()中,但话又说回来,我无法在其中设置BroadcastReceiver

Service类也是如此。 findViewById(R.id.imageView)有效,但无法运行Toast功能。

此时,我真的不知道还有什么可以尝试。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

谢谢Selvin帮助我。现在回答这个问题了。