问题描述
我已经完成了一个 android 应用程序,它可以存储带有人名和电话号码的日期.
I have already done one android application which stores date with person name and phone number.
现在我必须为此应用程序开发一个小部件,以通过按钮显示今天的(日期、人名和电话号码).
Now I have to develop one widget for this application to show today's (date, person name, and phone number) with button.
要求:
- 带有应用程序和小部件的单个应用程序.
- 当我点击小部件中的按钮时,它会启动我的应用程序.
- 小部件数据应始终与我的应用同步 - 当今天天(第 5 个人和应用程序再添加 5 个人然后小部件显示10个人资料)
- 如何设计这个小部件?有什么指导方针吗?我必须使用水平滚动视图.
我做了一个简单的小部件演示,但我不知道如何将它与我的应用程序同步.
I have done a simple widget demo but I don't know how to synchronize it with my application.
请分享您的经验并对我的小部件和应用程序提出一些想法.
Please share your experience and give some idea about my widget and application.
推荐答案
我发现我以某种方式找到了解决方案.所以我要分享我的想法.
I am finding couple of days I got somehow the solution. So I am sharing my idea.
这个网站对我很有用.
http://kasperholtze.com/android/how-to-make-a-simple-android-widget/
我做了一些改变并完成了我的工作.
I made some changes and did my work.
需求解决方案:
1.像这样制作接收器并放入清单权限.
1. Make Receiver and put into manifest permission like this.
<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.
2. In my side require top TextView click so can make using TextView id in 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. 我想调用数据库并获取记录并显示它.现在问题是更新怎么样?
3. I want to call database and fetch record and display it. Now question what about update?
所以我使用了计时器类并且每 1000 秒运行一次,因为小部件总是更新 30 分钟(可能是).
So I have used timer class and run every 1000sec because of over widget is always update 30min(may be).
@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]);
}
如果有任何疑问,请发表评论并感谢所有给予我全力支持以使此答案有用的人.
If any have queries then put a comment and thanks to all who gave me full support to make this answer useful.
这篇关于如何在单个应用程序中使用应用程序小部件创建 Android 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!