Har*_*hid 37 android-widget android-layout android-activity
我已经完成了一个Android应用程序,它存储了人名和电话号码的日期.
现在我必须为这个应用程序开发一个小部件,用按钮显示今天的(日期,人名和电话号码).
要求:
我做了一个简单的小部件演示,但我不知道如何将它与我的应用程序同步.
请分享您的经验,并对我的小部件和应用程序有所了解.
Roa*_*ies 36
创建和配置小部件
要注册窗口小部件,您需要为android.appwidget.action.APPWIDGET_UPDATE操作创建一个带有intent过滤器的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>
Run Code Online (Sandbox Code Playgroud)
您还可以通过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>
Run Code Online (Sandbox Code Playgroud)
widget_layout.xml在res/layout文件夹下定义以下文件.
<?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>
Run Code Online (Sandbox Code Playgroud)
现在上课.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我发现几天我得到了解决方案.所以我在分享我的想法.
这个网站对我来说非常有用.
http://kasperholtze.com/android/how-to-make-a-simple-android-widget/
我做了一些改变并做了我的工作.
要求解决方案:
1. 制作Receiver并将其置于明显许可之下.
<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>
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
3.我想调用数据库并获取记录并显示它.现在质疑更新的内容?
所以我使用了计时器类并且每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]);
}
Run Code Online (Sandbox Code Playgroud)
如果有任何查询,那么发表评论并感谢所有给予我全力支持的人,使这个答案有用.