私は最初のウィジェットに取り組んでいます.Android開発者のチュートリアルに従っています. 当初、ウィジェットはうまく機能していました。ウィジェット一覧に表示され、ホーム画面にドラッグすると、ホーム画面に表示されます。
しかし、いくつかのパラメーターを構成するには構成アクティビティが必要であり、そのために PreferenceActivity を使用しました。もう一度、ウィジェットがウィジェット リストに表示され、それをホーム画面にドラッグすると、PreferenceActivity が表示されます。しかし、(戻るボタンで) PreferenceActivity を終了すると、ウィジェットがホーム画面に表示されません。
私はインターネットを検索し、いくつかのアドバイス (メイン アクティビティの追加など) に従いましたが、これらの解決策はうまくいきませんでした。
以下は私のコードです。まず Android マニフェスト。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.my_widget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.my_widget.Main"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Preferences"
android:label="@string/title_activity_preferences" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<receiver android:name="com.example.my_widget.My_AppWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_info" />
</receiver>
</application>
</manifest>
ウィジェット プロバイダー:
public class My_AppWidgetProvider extends AppWidgetProvider {
private Context context;
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
this.context = context;
try {
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
int number = (new Random().nextInt(100));
// Set the text
remoteViews.setTextViewText(R.id.update, ""+number);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
create メソッドの設定アクティビティ:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
}
バックプレスされたメソッドでの設定アクティビティ:
@Override
public void onBackPressed() {
super.onBackPressed();
do_widget();
}
private void do_widget() {
try {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
RemoteViews views = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.widget);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
ウィジェット xml 情報ファイル:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure="com.example.my_widget.Preferences"
android:initialKeyguardLayout="@layout/widget"
android:initialLayout="@layout/widget"
android:minHeight="72dp"
android:minWidth="300dp"
android:previewImage="@drawable/ic_launcher"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="6000"
android:widgetCategory="home_screen|keyguard">
</appwidget-provider>