2013-02-23 139 views
37

我已經完成了一個android應用程序,其中存儲了包含人名和電話號碼的日期。如何在單個應用程序中創建帶應用程序小部件的android應用程序

現在我必須爲此應用程序開發一個小部件以顯示帶按鈕的今天(日期,人員姓名和電話號碼)。

要求:

  1. 與應用程序和widget單施。
  2. 當我點擊小部件中的按鈕時,它會啓動我的 應用程序。
  3. 控件的數據應與我的應用程序同步 - 今天,當 天(人5和應用程序中添加5人以上部件,然後顯示 10人的數據)
  4. 我如何設計這個小工具?任何指引?我必須使用水平滾動視圖 。

我已經做了一個簡單的小部件演示,但我不知道如何與我的應用程序同步。

請分享您的經驗,並提供一些關於我的小部件和應用程序的想法。

+0

我發現整天解決第一個和第二個要求,完成了。 – Harshid 2013-02-25 07:17:11

+4

http://www.vogella.com/articles/AndroidWidgets/article.html看到這個鏈接 – Roadies 2013-02-26 10:23:37

回答

8

我找到幾天我得到了某種解決方案。所以我分享我的想法。

此網站對我來說非常有用。

http://kasperholtze.com/android/how-to-make-a-simple-android-widget/

我做了一些改變,做我的工作。

需求的解決方案:

使接收器並投入像這樣明顯的權限。

<receiver 
     android:name=".WatchWidget" 
     android:label="WatchWidget" > 
     <intent-filter> 
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
     </intent-filter> 
     <meta-data 
      android:name="android.appwidget.provider" 
      android:resource="@xml/watch_widget_provider" /> 
    </receiver> 

2.在我的身邊需要頂級的TextView點擊等都可以在onUpdate()讓使用TextView的ID。

remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_lay); 

    Intent intent = new Intent(context, TestActivity.class); 
    intent.setAction("callActivity"); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
    remoteViews.setOnClickPendingIntent(R.id.widget_textview, pendingIntent); 
    appWidgetManager.updateAppWidget(new ComponentName(context,WatchWidget.class), remoteViews); 

我要調用數據庫並獲取記錄並顯示它。現在問題是什麼關於更新?

因此,我已經使用了定時器類,並且每1000秒運行一次,因爲以上小部件總是更新30分鐘(可能)。

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

    this.appWidgetManager = appWidgetManager; 
    try { 
     Timer timer = new Timer(); 
     timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager,appWidgetIds), 1, 1000); 

    } catch (Exception e) { 
     System.out.println("Exception:"); 
    } 
    updateSwitch1(context, appWidgetManager, appWidgetIds[0]); 

} 

如果有任何疑問,請發表評論,並感謝所有誰給了我全力支持,使這個答案有用。

+0

海我發佈這個鏈接的一個問題,請幫助我http://stackoverflow.com/questions/18376263/automatically-update-widget -in-the-home-screen-when-i-make-modification-in-my-ap – Satheesh 2013-08-22 12:06:39

+0

教程鏈接給出500內部服務器錯誤。 – Enoobong 2015-07-30 14:14:01

+1

鏈接不起作用 – baxx 2016-09-29 16:23:06

36

創建和配置窗口小部件

要註冊您創建的android.appwidget.action.APPWIDGET_UPDATE行動的意圖過濾器一個BroadcastReceiver一個小部件。

<receiver 
     android:icon="@drawable/icon" 
     android:label="Example Widget" 
     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> 

您還指定的元數據,通過Android窗口小部件:NAME =「android.appwidget.provider屬性由這個元數據所指的配置文件包含的配置設置如果包含例如更新界面,小部件的大小和初始佈局

在/ res/drawable目錄中創建一個新文件myshape.xml。該文件將定義我們在您的小部件中使用的背景。

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
      android:shape="rectangle" > 

    <stroke 
     android:width="2dp" 
     android:color="#FFFFFFFF" /> 

    <gradient 
     android:angle="225" 
     android:endColor="#DD2ECCFA" 
     android:startColor="#DD000000" /> 

    <corners 
     android:bottomLeftRadius="7dp" 
     android:bottomRightRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp" /> 

</shape> 

在res/layout文件夾下定義以下widget_layout.xml文件。

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/layout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="8dip" 
      android:background="@drawable/myshape" > 

     <TextView 
      android:id="@+id/update" 
      style="@android:style/TextAppearance.Medium" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:gravity="center_horizontal|center_vertical" 
      android:layout_margin="4dip" 
      android:text="Static Text" > 
     </TextView> 

    </LinearLayout> 

現在成爲班級。

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); 
    } 
    } 
+0

這對我有幫助@Maya mAku – Harshid 2013-03-05 10:27:07

+6

您忘記了abtout xml/widget_info文件,如: '<?xml version =「1.0」encoding =「utf-8」?> ' – Choletski 2016-01-21 09:16:08

相關問題