如何建立一个简单的Android窗口小部件 [英] How to build a Simple Android Widget
问题描述
所以我有一点经验,构建Android应用程序。但现在我想建立一个ANDROID小部件,将坐在主屏幕上显示一个按钮,该按钮时pressed它播放声音。我一直在看网上的教程就如何建立一个Android窗口小部件,但我似乎无法找到答案。是否有良好的教程在那里,就如何使一个独立的小部件这个简单的或者其他什么地方我可以开始? 提前致谢, 彼得·
So I have a bit of experience building android applications. But now i would like to build a widget for android that would sit on the home screen and display a button, and when the button is pressed it plays a sound. I've been looking at tutorials online on how to set up an android widget but i cant seem to figure it out. Are there any good tutorials out there on how to make a standalone widget this simple or somewhere i can start? Thanks in advance, Peter
推荐答案
第一创建内部资源/布局的新的布局文件,根据项目的结构,将定义根据以下结构的插件布局(widgetlayout.xml)。
first Create a new layout file inside res/layout, under the project structure, that will define the widget layout (widgetlayout.xml) according to the following structure.
<TextView android:text="@string/widgettext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:layout_gravity="center_vertical"
android:textColor="#000000"></TextView>
<TextView android:text="@string/widgetmoodtext"
android:id="@+id/widgetMood" android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:layout_gravity="center_vertical"
android:textColor="#000000"></TextView>
<ImageButton android:id="@+id/widgetBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:src="@drawable/smile_icon"
android:layout_gravity="center_vertical">
</ImageButton>
在项目结构创建RES / XML文件夹 用下列参数创建一个XML文件(widgetproviderinfo.xml):
Create the res/xml folder under the project structure Create a xml file (widgetproviderinfo.xml) with the following parameters:
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="220dp"
android:minHeight="72dp"
android:updatePeriodMillis="86400000"
android:initialLayout="@layout/widgetlayout">
</appwidget-provider>
现在你应该创建一个反应的笑脸图像按钮(CurrentMoodService.java)的用户交互服务
Now you should create the service that reacts to the user interaction with the smiley image button (CurrentMoodService.java).
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStart(intent, startId);
updateMood(intent);
stopSelf(startId);
return START_STICKY;
}
private void updateMood(Intent intent) {
if (intent != null){
String requestedAction = intent.getAction();
if (requestedAction != null && requestedAction.equals(UPDATEMOOD)){
this.currentMood = getRandomMood();
int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
AppWidgetManager appWidgetMan = AppWidgetManager.getInstance(this);
RemoteViews views = new RemoteViews(this.getPackageName(),R.layout.widgetlayout);
views.setTextViewText(R.id.widgetMood, currentMood);
appWidgetMan.updateAppWidget(widgetId, views);
}
}
}
定义了服务之后,现在是时候实现部件供应商类(CurrentMoodWidgetProvider.java)。
After defining the service, it is time to implement the widget provider class (CurrentMoodWidgetProvider.java).
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int i=0; i<appWidgetIds.length; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
Intent intent = new Intent(context, CurrentMoodService.class);
intent.setAction(CurrentMoodService.UPDATEMOOD);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.widgetBtn, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
最后,有必要申报服务和应用程序控件提供的清单(AndroidManifest.xml中)。
Finally it is necessary to declare the Service and the App Widget Provider in the Manifest (AndroidManifest.xml).
<service android:name=".CurrentMoodService">
</service>
<receiver android:name=".CurrentMoodWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widgetproviderinfo" />
</receiver>
如果你想下载整个源$ C $ C,那么看看下面的网址...
and if you wants to download the whole of the source code then have a look at the url below...
<一个href="http://sites.google.com/site/androidsource$c$c/src/CurrentMoodWidgetProject.rar?attredirects=0" rel="nofollow">http://sites.google.com/site/androidsource$c$c/src/CurrentMoodWidgetProject.rar?attredirects=0
这篇关于如何建立一个简单的Android窗口小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!