Поэтому я много часов пытался заставить это работать, и я не могу, чтобы жизнь меня поняла. Я пробовал много разных идей, которые я нашел, просто искал его, но без везения. Я пытаюсь создать виджет Android, который вы можете щелкнуть по изображению, и он использует браузер по умолчанию для открытия веб-сайта. Я могу правильно настроить изображение и виджет, но когда я пытаюсь реализовать код кнопки для открытия веб-сайта, он убивает виджет и говорит "Проблема загрузки гаджета" по телефону. Я новичок в создании виджетов/приложений, поэтому любые советы приветствуются. Как я могу заставить виджет работать?
Вот мой код:
main.xml
$<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/widget_frame_portrait"
>
<TextView android:id="@+id/xmas"
android:layout_toRightOf="@+id/refresh"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:text="@string/hello"
android:layout_marginTop="37dip"
android:layout_marginBottom="25dip"
android:layout_marginRight="5dip"
android:orientation="vertical"
android:textColor="#000000"/>
<ImageButton
android:layout_height="50dip"
android:id="@+id/refresh"
android:background="@drawable/feep"
android:layout_width="225dip"
android:layout_marginTop="25dip"
android:layout_marginBottom="25dip"
android:layout_marginLeft="25dip"
android:layout_marginRight="1dip"
android:orientation="horizontal"
android:clickable="true"
android:onClick="openWebUrl"
/>
</RelativeLayout>
Моя единственная деятельность, называемая bGiving.Java:
$package chris.days.to;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
public class bGiving extends Activity implements View.OnClickListener {
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
}
public void openWebURL(View v) {
String url = "http://google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
public void onClick(View v) {
}
}
and my Android Manifest is:
$<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="chris.days.to"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".DaysToXmas" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/hello_widget_provider" />
</receiver>
<activity android:name=".bGiving"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Когда я добавляю код в main.xml "android: onClick =" openWebUrl ", он убивает виджет.
Установите PendingIntent
на Button
. Это приведет к тому, что Intent
будет выполняться при нажатии Button
.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(data);
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
remoteView.setOnClickPendingIntent(R.id.button, pendingIntent);