2015-06-21 13 views
0

我正在嘗試構建一個hello世界小部件,並且我遵循了三個不同的示例,每次我的apk都將主要活動加載爲應用程序而不是小部件。在最後一個例子中,它顯示了沒有活動塊的清單。當我將主要活動從清單中取出時,只需要接收器塊Android Studio就會拋出未找到Default活動的異常。這就是我從這個例子中得出的一個區別。我在清單中有一個< *活動>阻止。小部件作爲應用程序加載

我在Android Studio 1.0.2中。什麼可能導致這個?

我當前的代碼是基於本實施例中 http://www.vogella.com/tutorials/AndroidWidgets/article.html

http://www.sitepoint.com/how-to-code-an-android-widget/

https://looksok.wordpress.com/2012/12/15/android-complete-widget-tutorial-including-source-code/

AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.me.countdown" > 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

    </activity> 
    <receiver android:name="MyWidgetProvider" > 
     <intent-filter > 
      <action 
       android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
     </intent-filter> 

     <meta-data 
      android:name="android.appwidget.provider" 
      android:resource="@xml/widget_info" /> 
    </receiver> 
</application> 

</manifest> 

MyWidgetProvider.xml

下水庫
public class MyWidgetProvider extends AppWidgetProvider { 

private static final String ACTION_CLICK = "ACTION_CLICK"; 

@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
        int[] appWidgetIds) { 

    // Get all ids 
    ComponentName thisWidget = new ComponentName(context, 
      MyWidgetProvider.class); 
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget); 
    for (int widgetId : allWidgetIds) { 
     // create some random data 
     int number = (new Random().nextInt(100)); 

     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), 
       R.layout.widget_layout); 
     Log.w("WidgetExample", String.valueOf(number)); 
     // Set the text 
     remoteViews.setTextViewText(R.id.update, String.valueOf(number)); 

     // Register an onClickListener 
     Intent intent = new Intent(context, MyWidgetProvider.class); 

     intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); 
     intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); 

     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 
       0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent); 
     appWidgetManager.updateAppWidget(widgetId, remoteViews); 
    } 
} 
} 

widget_info.xml/XML

\<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:initialLayout="@layout/widget_layout" 
android:minHeight="72dp" 
android:minWidth="300dp" 
android:updatePeriodMillis="300000" > 

</appwidget-provider> 

回答

0

我不知道那些其他的例子是專爲,但我使用的Android Studio版本 - 1.2.2 - 它不同。我找到了下面的例子,並且能夠獲得一個小工具的工作shell,這正是我想要的。即使這個在編譯和運行之前需要很多小的調整,但我確實得到了它。

格雷格

http://www.clearcreekcode.com/create-a-widget-in-android-studio/

相關問題