2015-11-19 117 views
0

快速的問題,因爲我不能自己搞清楚。更改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()函數。

在這一點上,我真的不知道還有什麼可以嘗試的。有任何想法嗎?

+0

'如果使用者清潔「最近用過的應用」(或任何其它類型的存儲器的清洗,即CleanMaster) - AlarmManager Stops'然後因爲活動沒有運行裝置當然得到NPE錯誤'mImageView'是'null'並且還' MainActivity.this'將給出無效的上下文錯誤 –

+1

好吧,現在我離開了工作,所以我可以構建最小的例子...這是做這件事的最小方式(沒有任何庫,並應該在API> 5 ... https://gist.github.com/SelvinPL/be80f168797517291c81 – Selvin

+0

@Selvin我非常驚訝,你如何設法想出這5分鐘我張貼的問題後,雖然我與它掙扎了三天。 我欣賞代碼,我從中學到了一些新東西。 PS我可以使用它並添加一些我的代碼嗎?它比我的更乾淨,而且我所有的東西都是我的ded是InputStream使我自己的代碼工作。 :) 謝謝, – xsonz

回答

0

謝謝Selvin幫助我。現在回答這個問題。