【问题标题】:Widget with autocomplete TextView具有自动完成 TextView 的小部件
【发布时间】:2012-03-28 07:06:12
【问题描述】:

我想创建一个小部件,单击它以打开带有自动完成文本视图的对话框(来自 main.class)并执行 mainclass 中的函数。这是我的小部件类,请告诉我在 android manifest 中也放什么.谢谢

 public class AppWidget extends AppWidgetProvider
 {
@Override
public void onReceive(Context ctxt, Intent intent)
{
    if(intent.getAction()==null)
    {
        ctxt.startService(new Intent(ctxt,ToggleService.class));
    }
    else
    {
        super.onReceive(ctxt, intent);
    }

}

@Override
public void onUpdate(Context context,AppWidgetManager appWidgetManager, int [] appWidgetIds)
{
    context.startService(new Intent(context,ToggleService.class));
    //RemoteViews buildUpdate(context);
}

public static class ToggleService extends IntentService
    {
    public ToggleService() {
        super("AppWidget$ToggleService");

        }



@Override
protected void onHandleIntent(Intent intent)
{
    ComponentName me = new ComponentName(this,AppWidget.class);
    AppWidgetManager mgr= AppWidgetManager.getInstance(this);
    mgr.updateAppWidget(me,buildUpdate(this));
}

private RemoteViews buildUpdate(Context context)
{
    RemoteViews updateViews=new RemoteViews(context.getPackageName(),R.layout.widget);


    Intent i=new Intent(this, AppWidget.class);
    PendingIntent pi= PendingIntent.getBroadcast(context,0, i,0);
    updateViews.setOnClickPendingIntent(R.id.phoneState,pi);
    return updateViews;
}

} }

widgetxml//

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView 

     android:id="@+id/phoneState"

     android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_launcher"
    />



</RelativeLayout>

// 和 res/xml 中的 widget_provider.xml

     <?xml version="1.0" encoding="utf-8"?>
    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"

    android:minWidth="79px"
    android:minHeight="79px"
    android:updatePeriodMillis="1800000"
    android:initialLayout="@layout/widget">


<TextView

 android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Loading..." />

// 和我的清单的一部分

   <receiver android:name=".AppWidget"
        android:label="Caller"
        android:icon="@drawable/ic_launcher" >
        <intent-filter >
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />

        </intent-filter>
        <meta-data 
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_provider"
            />

    </receiver>
    <service android:name=".AppWidget$ToggleService"  />   

【问题讨论】:

  • 你的标题太长了......不要使用像“任何人都可以参考一些例子我应该怎么做”这样的关键字
  • 嗨,托尼,我是伊姆兰。你想启动一个服务或活动?
  • 当我按下小部件时,我想使用我的自动完成文本视图(它搜索联系人并在单击时调用它们)所以无论如何,当单击小部件以显示自动完成时,这是可能的(因为自动完成..cant成为一个小部件)并帮助我执行 Main.class 中自动完成执行的命令...thx

标签: android autocomplete android-widget widget textview


【解决方案1】:

更新你的 manifest.xml

<receiver android:name=".AppWidget"
        android:label="Caller"
        android:icon="@drawable/ic_launcher" >
        <intent-filter >
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
 <action android:name="com.app.example.MyWidget.ACTION_WIDGET_CLICK_RECEIVER"/>

        </intent-filter>
        <meta-data 
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_provider"
            />

    </receiver>
    <service android:name=".AppWidget$ToggleService"  />   

并更新您的AppWidgetProvider

public class MyWidget extends AppWidgetProvider {
        public static String ACTION_WIDGET_CLICK_RECEIVER = "ActionReceiverWidget";

        public static int appid[];
        public static RemoteViews rview;
        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
                int[] appWidgetIds){
            updateWidgetState(context, ""); 
        }
        @Override
        public void onReceive(Context paramContext, Intent paramIntent)
          {
             String str = paramIntent.getAction();
            if (paramIntent.getAction().equals(ACTION_WIDGET_CLICK_RECEIVER)) {
                updateWidgetState(paramContext, str);   
            }
            else
            {
                    if ("android.appwidget.action.APPWIDGET_DELETED".equals(str))
                      {
                        int i = paramIntent.getExtras().getInt("appWidgetId", 0);
                        if (i == 0)
                        {

                        }
                        else
                        {
                            int[] arrayOfInt = new int[1];
                            arrayOfInt[0] = i;
                            onDeleted(paramContext, arrayOfInt);
                        }
                      }
              super.onReceive(paramContext, paramIntent);
            }
          }
         static void updateWidgetState(Context paramContext, String paramString)
          {
            RemoteViews localRemoteViews = buildUpdate(paramContext, paramString);
            ComponentName localComponentName = new ComponentName(paramContext, MyWidget.class);
            AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, localRemoteViews);
          }
         private static RemoteViews buildUpdate(Context paramContext, String paramString)
          {
            // Toast.makeText(paramContext, "buildUpdate() ::"+paramString, Toast.LENGTH_SHORT).show();
            rview = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layout);
            Intent active = new Intent(paramContext, MyWidget.class);
            active.setAction(ACTION_WIDGET_RECEIVER);
            active.putExtra("msg", "Message for Button 1");
            PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext, 0, active, 0);
            rmViews.setOnClickPendingIntent(R.id.buttonus1, configPendingIntent);
            if(parmString.equals(ACTION_WIDGET_CLICK_RECEIVER))
            {

               //open a dialog with a autocompletetextview
             //your code for update and what you want on button click

            }  
             return rview; 
          }
        @Override
        public void onEnabled(Context context){
            super.onEnabled(context);
           // Toast.makeText(context, "onEnabled()  ", Toast.LENGTH_SHORT).show();
        }
        // Called each time an instance of the App Widget is removed from the host
        @Override
        public void onDeleted(Context context, int [] appWidgetId){
            super.onDeleted(context, appWidgetId);
           // Toast.makeText(context, "onDeleted()  ", Toast.LENGTH_SHORT).show();
        }
        // Called when last instance of App Widget is deleted from the App Widget host.
        @Override
        public void onDisabled(Context context) {
            super.onDisabled(context);
           // Toast.makeText(context, "onDisabled()  ", Toast.LENGTH_SHORT).show();
        }

    }

【讨论】:

  • 但是托尼昨天告诉你,对话框不能通过小部件点击打开。
  • 因为我的 pgk 名称是清单中的 dd.caller 我应该写 ??
  • 我只需要 AppWidgetProvider 中的代码来打开一个对话框,我会在对话框中写下所有内容,如果对话框是一个透明的活动,请不要介意如何通过小部件
  • 我决定这样做,我正在制作新的活动 caller2.class,你能告诉我当按下小部件时如何调用 caller2.class(它是透明的)而不是 CallerActivity.class跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-17
  • 2017-09-13
  • 2011-04-11
  • 1970-01-01
  • 2011-11-18
  • 2012-08-09
相关资源
最近更新 更多