早上好每个人, 试图让小部件上的onclick事件起作用,问题是在设置挂起意图的onUpdate函数之前没有完成后台asynctask。我对android有一点熟悉,但绝对还处于学习阶段。我在实际活动中所做的测试运行良好且正确,并且观察日志何时进入它肯定是一个时间问题。任何帮助将不胜感激!!
public class SingleClickWanIP extends AppWidgetProvider {
private static final String TAG = "waninfo";
private String myURL;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
Log.d(TAG,"started update: " + myURL);
WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();
String homessid = "\"MyRouterName\"";
if (ssid.equalsIgnoreCase(homessid)){
myURL = "192.168.1.106";
}
Log.d(TAG,"finished ssd check: " + myURL);
String newURL = "http://"+myURL+":120";
Uri uri = Uri.parse(newURL);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.single_click_wan_ip);
Log.d(TAG,"finished intent and remote views: " + myURL);
views.setOnClickPendingIntent(R.id.ibOrange, pendingIntent);
Log.d(TAG,"finished on click pending: " + myURL);
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
}
Log.d(TAG,"finished onUpdate: " + myURL);
}
@Override
public void onEnabled(Context context) {
ProcessURL process = new ProcessURL();
process.execute("https://www.dropbox.com/link to get external ip value/wanip.txt?raw=1");
Log.d(TAG,"started process: " + myURL);
}
private class ProcessURL extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
Log.d(TAG,"started background");
return GET(params[0]);
}
@Override
protected void onPostExecute(String result) {
myURL = result;
Log.d(TAG,"finished and set myURL variable: " + myURL);
}
private String GET(String url){
InputStream inputStream = null;
String result = "";
try {
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
}
return result;
}
private String convertInputStreamToString(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
}
一点点背景,我很便宜并且不想支付no-ip或类似的东西。但我想与朋友和家人分享我的服务器访问权限,而不必告诉他们&#34;是的,只需输入xx.xx.xx.xxx:120即可到达那里&#34;。所以服务器写入我的Dropbox文件外部ip,我有一个来自Dropbox的共享链接,我在这个应用程序中使用它来获取值,然后打开我的服务器的站点。中提琴
......得到了!摸索着我发现这篇文章关于小部件自发切换的奇怪行为修复了spontaneous widget。然后单击将不起作用,因为我必须使用if包装onupdate中的代码,因为它不会创建一个挂起的uri意图,其中null为url地址。所以...将挂起的onclick意图复制到onpostexecute进程,现在我是地毯中的一个错误。
感谢领导,非常感谢!
WifiManager wifiManager = (WifiManager) gContext.getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();
String homessid = "\"MyRouterName\"";
if (ssid.equalsIgnoreCase(homessid)) {
result = "192.168.1.106";
}
Log.d(TAG, "finished ssd check: " + result);
myURL = "http://" + result + ":8080";
Uri uri = Uri.parse(myURL);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
PendingIntent pendingIntent = PendingIntent.getActivity(gContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
RemoteViews views = new RemoteViews(gContext.getPackageName(), R.layout.single_click_wan_ip);
Log.d(TAG, "finished intent and remote views: " + myURL);
views.setOnClickPendingIntent(R.id.ibOrange, pendingIntent);
Log.d(TAG, "finished on click pending: " + myURL);
ComponentName thisWidget = new ComponentName(gContext, SingleClickWanIP.class);
gappWidgetManager.updateAppWidget(thisWidget, views);
Log.d(TAG,"finished and set myURL variable: " + myURL);
答案 0 :(得分:1)
我会请求更新asynctask onPostExecute
上的小部件,因为那时你有了URL,现在可以正确设置挂起的意图了。
使用具有操作AppWidgetManager.ACTION_APPWIDGET_UPDATE的intent来请求窗口小部件更新。
答案 1 :(得分:0)
在您的情况下,从IntentService
更新小部件更安全,而不是从应用小部件提供商启动的AsyncTask
更新。
onEnabled
和onUpdate
应该在(最多)10秒内完成。
我强烈建议您使用以下代码:
<强> SingleClickWanIP.java 强>
public class SingleClickWanIP extends AppWidgetProvider
{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
context.startService(new Intent(context, ProcessURL.class));
}
@Override
public void onEnabled(Context context)
{
context.startService(new Intent(context, ProcessURL.class));
}
}
<强> ProcessURL.java 强>
public class ProcessURL extends IntentService
{
private static final String TAG = "waninfo";
private String myURL;
public ProcessURL()
{
super("ProcessURL__service");
setIntentRedelivery(true);
}
@Override
protected void onHandleIntent(Intent intent__)
{
myURL = GET("https://www.dropbox.com/link to get external ip value/wanip.txt?raw=1");
Log.d(TAG,"finished and set myURL variable: " + myURL);
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();
String homessid = "\"MyRouterName\"";
if (ssid.equalsIgnoreCase(homessid))
{
myURL = "192.168.1.106";
}
Log.d(TAG,"finished ssd check: " + myURL);
myURL = "http://"+myURL+":120";
Uri uri = Uri.parse(myURL);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
RemoteViews views = new RemoteViews(getPackageName(), R.layout.single_click_wan_ip);
Log.d(TAG,"finished intent and remote views: " + myURL);
views.setOnClickPendingIntent(R.id.ibOrange, pendingIntent);
Log.d(TAG,"finished on click pending: " + myURL);
// Update all widgets
AppWidgetManager manager = AppWidgetManager.getInstance(this);
if(null != manager)
{
Log.d(TAG,"Updating all widgets...");
manager.updateAppWidget(new ComponentName(this,SingleClickWanIP.class), views);
Log.d(TAG,"finished and set myURL variable: " + myURL);
}
}
private String GET(String url)
{
InputStream inputStream = null;
String result = "";
try
{
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
}
catch (Exception e)
{
Log.d(TAG, e.getLocalizedMessage());
}
return result;
}
private String convertInputStreamToString(InputStream inputStream) throws IOException
{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
}
在AndroidManifest.xml文件的<application />
标记中添加以下行:
<service android:name="[ADD-THE-PATH-TO-ProcessURL.java-HERE].ProcessURL" android:enabled="true" android:exported="false" />