【发布时间】: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