Android 4.XX jellybean上未显示应用程序小部件

时间:2014-02-24 08:09:42

标签: android android-appwidget appwidgetprovider

我有一个应用程序,我也为它创建了一个应用程序小部件。 app小部件只是启动应用程序的快捷方式。

我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hansem.handbookmanual"
    android:installLocation="internalOnly"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:exported="true"
        android:theme="@style/AppTheme" >

        <receiver android:name="com.example.HandbookAppWidgetProvider" android:label="abc">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget1_info" />
        </receiver>

        <activity
            android:name="com.example.BrowserLauncherActivity"
            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>

Widget1_info.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:minWidth="72dp"
    android:minHeight="72dp"
    android:minResizeHeight="72dp"
    android:resizeMode="vertical"
    android:updatePeriodMillis="0"
    android:initialLayout="@layout/widget1">

</appwidget-provider>

Widget1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/button_applaunch"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="4dp"
    android:background="@drawable/ic_launcher"
    android:gravity="center"
    android:orientation="vertical" >

</LinearLayout>

我已经尝试了Stackflow上提到的所有解决方案,但注意到了帮助。在重新启动设备时,窗口小部件会被添加,但通常只需在手机上安装应用程序并启动它,就不会添加窗口小部件。

AppWidgetProviderCode:

public class HandbookAppWidgetProvider extends AppWidgetProvider {
  DateFormat df = new SimpleDateFormat("hh:mm:ss");

  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;

    Log.i("ExampleWidget",  "Updating widgets " + Arrays.asList(appWidgetIds));

    // Perform this loop procedure for each App Widget that belongs to this
    // provider
    for (int i = 0; i < N; i++) {
      int appWidgetId = appWidgetIds[i];

      // Create an Intent to launch ExampleActivity
      Intent intent = new Intent(context, BrowserLauncherActivity.class);
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

      // Get the layout for the App Widget and attach an on-click listener
      // to the button
      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget1);
      views.setOnClickPendingIntent(R.id.button_applaunch, pendingIntent);

      // To update a label
      //views.setTextViewText(R.id.widget1label, df.format(new Date()));

      // Tell the AppWidgetManager to perform an update on the current app
      // widget

      appWidgetManager.updateAppWidget(appWidgetId, views);
    }
  }
}

请帮忙

0 个答案:

没有答案