我已经做存储最新与人姓名和电话号码一个Android应用程序 。
现在,我要开发一个插件这个应用程序显示今日(日期,人名和电话号码)按钮。
要求:
- 与应用程序和widget单一的应用程序。
- 当我点击一个按钮,在窗口小部件,它将开始我的应用程序。
- 窗口小部件的数据应与我的应用程序同步 - 当今天的日子(5人及应用程序中添加5人以上部件,然后显示10人的数据)
- 我如何设计这个小工具? 任何指引? 我必须使用水平滚动型。
我做了一个简单的小工具的演示,但我不知道如何将它与我的应用程序同步。
请分享你的经验,给我的小部件和应用程序的一些想法。
创建和配置窗口小部件
要注册您创建的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 /绘制目录的新文件myshape.xml。 这个文件将定义我们在你的widget使用的背景。
<?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>
定义以下widget_layout.xml
的RES /布局文件夹下的文件。
<?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);
}
}
我发现几天我莫名其妙的解决方案。 因此,我分享我的想法。
本网站已对我来说非常有用。
http://kasperholtze.com/android/how-to-make-a-simple-android-widget/
我做了一些改变,做我的工作。
需求的解决方案:
1.接收并投入像这样明显的权限。
<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点击这样可以使使用TextView的ID中onUpdate()
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);
3.我要调用数据库并获取记录并显示它。 现在怀疑什么更新?
所以,我已经使用Timer类和运行,因为过小部件是随时更新30分钟(可能)每1000sec。
@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]);
}
如果有任何疑问都然后放一个注释,感谢所有谁给了我充分的支持,使这个答案非常有用。