gpt4 book ai didi

java - 尝试在服务中处理不同的场景

转载 作者:行者123 更新时间:2023-11-30 01:45:02 25 4
gpt4 key购买 nike

我有一个问题,我想不出如何解决它。

目标是,当用户点击按钮时,根据在设置中选择的内容加载 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 Thread 中并将代码放在那里。然后,我调用了 mainActivity.getPhoto();在服务中。问题是,我收到一个错误 - “尝试在空对象引用上调用虚拟方法‘int android.widget.EditText.length()’”

关于我应该做什么的任何想法?

所有荣耀的完整代码:

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));
}
}
}
}

谢谢:)

最佳答案

首先,您应该永远不要自己实例化一个 Activity 。

其次,作为最佳实践,您的服务不应该知道您的 Activity ,或者它有编辑文本。相反,当创建 PendingIntent 时,您应该发送 URL 以加载到您的 Intent 中,如下所示:

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");
// ...
}

关于java - 尝试在服务中处理不同的场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33857861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com