快速的问题,因为我不能自己搞清楚。更改ImageView图片BroadcastReceiver内
首先,我并不想出来的乞丐,所以我会告诉你我所知道的。
这段代码的伟大工程:
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
的另一个Activity
。
我也试过把getImage()
函数放在BroadcastReceiver
里,但是后来我又不能在其中设置findViewById(R.id.imageView)
。
Service类也是一样。 Toast
工作,但不能运行getImage()
函数。
在这一点上,我真的不知道还有什么可以尝试的。有任何想法吗?
'如果使用者清洁“最近用过的应用”(或任何其它类型的存储器的清洗,即CleanMaster) - AlarmManager Stops'然后因为活动没有运行装置当然得到NPE错误'mImageView'是'null'并且还' MainActivity.this'将给出无效的上下文错误 –
好吧,现在我离开了工作,所以我可以构建最小的例子...这是做这件事的最小方式(没有任何库,并应该在API> 5 ... https://gist.github.com/SelvinPL/be80f168797517291c81 – Selvin
@Selvin我非常惊讶,你如何设法想出这5分钟我张贴的问题后,虽然我与它挣扎了三天。 我欣赏代码,我从中学到了一些新东西。 PS我可以使用它并添加一些我的代码吗?它比我的更干净,而且我所有的东西都是我的ded是InputStream使我自己的代码工作。 :) 谢谢, – xsonz