2015-11-22 47 views
1

我遇到了问题,并且我没有想法如何解决它。试图处理服务中的不同场景

目标是,当用户点击按钮时,根据设置中选择的内容加载URL。

问题是,我无法正确设置它。

逻辑上(对我来说),我试图在服务中设置它。点击按钮>服务开始> URL从“IF ELSE”加载。 问题是,我得到一个错误“IF ELSE” - “方法时长必须从UI线程调用,目前推断线程是工人

public static class Service extends IntentService { 
    public Service() { 
     super("wallpaperchanger-download"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     MainActivity mainActivity; 
     mainActivity = new MainActivity(); 

     if (mainActivity.mEditTextHashtag.length() > 2) { 

      WallpaperManager wm = WallpaperManager.getInstance(this); 
      int height = wm.getDesiredMinimumHeight(); 
      int width = wm.getDesiredMinimumWidth(); 

      String url = "https://source.unsplash.com/all/?" + mainActivity.mEditTextHashtag.getText() + "/" + width + "x" + height + "/"; 
      try { 
       InputStream input = new URL(url).openStream(); 
       Log.v(TAG, url); 
       wm.setStream(input); 
       input.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      loading = false; 
     } 

    } 
} 

好,不够公平 我创造了新的方法getPhoto。 ();在UI线程中,并把代码放在那里,然后,我调用mainActivity.getPhoto();在服务 问题是,我得到一个错误 - “试图调用虚拟方法'int android.widget.EditText.length ()”对空对象引用”

上我应该做的

的完整代码的任何想法? LL它的辉煌:

package com.edip.splashwallpaper; 

import android.app.AlarmManager; 
import android.app.IntentService; 
import android.app.PendingIntent; 
import android.app.WallpaperManager; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Spinner; 
import android.widget.Switch; 
import android.widget.Toast; 

import java.io.InputStream; 
import java.net.URL; 

public class MainActivity extends android.app.Activity { 

    final static String TAG = "AllInOne"; 
    final static int CHANGE_INTERVAL = 30 * 1000; //30 sec for testing 
    static boolean loading = false; 
    WallpaperManager wm; 

    //Layout Views 
    Switch mSwitchFixedPhoto, mSwitchControls, mSwitchSave, mSwitchPause; 
    Spinner mSpinnerCategories, mSpinnerInterval; 
    EditText mEditTextHashtag; 
    Button mWallpaperButton; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //Layout Views Initialized 
     mSwitchFixedPhoto = (Switch) findViewById(R.id.sw_fixedphoto); 
     mSwitchControls = (Switch) findViewById(R.id.switch_controls); 
     mSwitchSave = (Switch) findViewById(R.id.switch_save); 
     mSwitchPause = (Switch) findViewById(R.id.switch_pause); 
     mSpinnerCategories = (Spinner) findViewById(R.id.spinner_categories); 
     mSpinnerInterval = (Spinner) findViewById(R.id.spinner_interval); 
     mEditTextHashtag = (EditText) findViewById(R.id.et_hashtag); 
     mWallpaperButton = (Button) findViewById(R.id.btn_set_wallpaper); 


     // Create an ArrayAdapter using the string array and a default spinner layout 
     ArrayAdapter<CharSequence> adapterCategory = ArrayAdapter.createFromResource(this, 
       R.array.categories_array, R.layout.dialog_spinner_layout); 
     // Specify the layout to use when the list of choices appears 
     adapterCategory.setDropDownViewResource(R.layout.dialog_spinner_layout); 
     // Apply the adapter to the spinner 
     mSpinnerCategories.setAdapter(adapterCategory); 

     ArrayAdapter<CharSequence> adapterInterval = ArrayAdapter.createFromResource(this, 
       R.array.interval_array, R.layout.dialog_spinner_layout); 
     adapterInterval.setDropDownViewResource(R.layout.dialog_spinner_layout); 
     mSpinnerInterval.setAdapter(adapterInterval); 

     mWallpaperButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       PendingIntent pending = PendingIntent.getBroadcast(MainActivity.this, 
         666, new Intent("com.edip.splashwallpaper.CHANGE_WALLPAPTER_TIMER"), 
         PendingIntent.FLAG_CANCEL_CURRENT); 

       ((AlarmManager) getSystemService(Context.ALARM_SERVICE)) 
         .setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 
           CHANGE_INTERVAL, pending); 

      } 
     }); 

    } 

    public void getPhoto() { 

     if (mEditTextHashtag.length() > 2) { 

      wm = WallpaperManager.getInstance(this); 
      int height = wm.getDesiredMinimumHeight(); 
      int width = wm.getDesiredMinimumWidth(); 

      String url = "https://source.unsplash.com/all/?" + mEditTextHashtag.getText() + "/" + width + "x" + height + "/"; 
      try { 
       InputStream input = new URL(url).openStream(); 
       Log.v(TAG, url); 
       wm.setStream(input); 
       input.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      loading = false; 

     } else { 

      Toast.makeText(this, "Something else", Toast.LENGTH_SHORT).show(); 

     } 

    } 

    public static class Service extends IntentService { 
     public Service() { 
      super("wallpaperchanger-download"); 
     } 

     @Override 
     protected void onHandleIntent(Intent intent) { 
      MainActivity mainActivity; 
      mainActivity = new MainActivity(); 

      mainActivity.getPhoto(); 
     } 
    } 

    public static class AlarmReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(final Context context, Intent intent) { 
      if (!loading) { 
       loading = true; 
       context.startService(new Intent(context, Service.class)); 
      } 
     } 
    } 
} 

谢谢:)

回答

1

首先,你应该永远通过自己实例化的活动。

其次,作为一种最佳实践,您的服务不应该了解您的活动,或者它有一个编辑文本。相反,你应该将此网址发送给您的意图内加载,当创建的PendingIntent,就像这样:

Intent intent = new Intent("com.edip.splashwallpaper.CHANGE_WALLPAPTER_TIMER"); 
intent.putExtra("USER_URL", "https://source.unsplash.com/all/?" + mEditTextHashtag.getText() + "/" + width + "x" + height + "/"); 
PendingIntent pending = PendingIntent.getBroadcast(MainActivity.this, 
        666, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

那么你的服务中,读取URL像这样:

@Override 
protected void onHandleIntent(Intent intent) { 
    String url = intent.getStringExtra("USER_URL"); 
    // ... 
} 
+0

感谢您的信息。我会牢记这一点。 我设法根据我的喜好设置一切,但有点不同。 您的想法使用putExtra是我需要的。 – xsonz