GVKun编程网logo

android – 我可以在appWidget中使用哪些视图?(appwidgetsmith)

9

本文将介绍android–我可以在appWidget中使用哪些视图?的详细情况,特别是关于appwidgetsmith的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同

本文将介绍android – 我可以在appWidget中使用哪些视图?的详细情况,特别是关于appwidgetsmith的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于android app widget 中使用自定义 view、android appwidget 笔记、Android AppWidgetProviderInfo图标ID、Android AppWidget开发的知识。

本文目录一览:

android – 我可以在appWidget中使用哪些视图?(appwidgetsmith)

android – 我可以在appWidget中使用哪些视图?(appwidgetsmith)

任何人都可以告诉我在appWidget中可以使用哪些视图?

谢谢!

解决方法:

请参阅Android开发者网站上的app widgets article,以获取可在窗口小部件中使用的布局和视图的列表.

android app widget 中使用自定义 view

android app widget 中使用自定义 view

app widget 开发中,我想 使用自己定义的 view, 因为 app widget 支持 RemoteViews, 我想问的是 RemoteViews 中怎么样使用自己的 view, 如自定义 view 是 com.my.AwView, 使用代码怎么样加载这个 view 呢?

android appwidget 笔记

android appwidget 笔记

AppWidget 就是我们使用的窗口小部件

实现 appwidget 非常简单,只需要一下几个步骤就 OK

1. 在 /res/layout 中建立一个 布局文件。此布局文件就是 窗体小部件 的 "样子"

2. 在 /res/xml 中建立一个 xml 文件此文件对 appwidget 进行配置。如下

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:minWidth="1dp"	
    android:minHeight="1dp"
    android:updatePeriodMillis="4000000"
    android:initialLayout="@layout/appwidgetlayout">
    <!-- 从上到下
    	宽
    	高
    	更新毫秒数
    	appwidget的初始化的布局 引用 res/layout 中的布局
     -->    
</appwidget-provider>

3. 实现一个类继承 AppWidgetProvider 

public class AppWeigetTest extends AppWidgetProvider {
	private static final String in="joker.broadcast.appwedget";
	//当到达指定的更新时间之后,或用户添加窗口小部件时触发
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onUpdate(context, appWidgetManager, appWidgetIds);		
	}
	//当用户删除窗体部件时触发
	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		// TODO Auto-generated method stub	
	}
	//当所有船体部件删除时触发
	@Override
	public void onDisabled(Context context) {
		// TODO Auto-generated method stub
	}
	//接收到广播的时候触发,以上几个方法是使用这个方法转发的,也就是说其实窗口的各种"事件" 都是使用广播的形式发出的
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		super.onReceive(context, intent);
	}
}
4. 在 AndroidManifest.xml

<receiver android:name=".AppWeigetTest">
             <intent-filter>
                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>                 
             </intent-filter>
             <meta-data android:name="android.appwidget.provider"
                 android:resource="@xml/appwidget_info"/>
         </receiver>
以上 intent-filter 和 meta-data 是系统设置不变的,其中 meta-data 的 resource 下的文件是 appwidget 的配置文件

二,Appwidget 中常用类的使用:

1.PendingIntent  

           此类是包装 Intent 类的 使 Intent 在特定的事件触发    

PendingIntent包装Intent意图 初始化方法有:getActivity getBroadcast getService
//参1 上下文对象 参数3 Intent对象
PendingIntent pending=PendingIntent.getActivity(context, 0, inten, 0);
2.RemoteViews

         此类可以对:"小部件" 上的控件进行操作 (比如改变 TextView 上的文字 ImageView 上的图片操作,改变图片添加事件) 

RemoteViews remote=new RemoteViews(context.getPackageName(), R.layout.appwidgetlayout);
			//为窗口小部件上的控件添加点击事件
			//参数一: 指定窗口小部件上的控件ID
			//参数2 :指定触发事件后执行的Intent 的PendingIntent
remote.setOnClickPendingIntent(R.id.button1, pending); //像button1添加一个点击事件
3 AppWidgetManager

在 onXXX 事件中做为参数传递进来的,此类的对象负责更新 Appwidget 

			AppWidgetManager manager=AppWidgetManager.getInstance(context);    
			ComponentName name=new ComponentName(context, AppWeigetTest.class);//代表AppWeiget控件
			//以上两个步骤是在没有appwidgetmanager对象的时候,比如在onReceive方法里如何使用
			manager.updateAppWidget(name, remoteViews); //此方法更新appwidget并且执行remoteViews只有执行这个方法后remoteViews配置的信息才会生效



Android AppWidgetProviderInfo图标ID

Android AppWidgetProviderInfo图标ID

如何解决Android AppWidgetProviderInfo图标ID?

| Android的“ 0”类包含一个整数“ 1”字段。我想将图标保留为
Drawable
。 我认为图标ID需要针对某些软件包的ѭ3进行解析。窗口小部件提供程序不是我的应用程序的一部分,因此
getResources()
返回的我自己的应用程序资源无用。 resources5ѭ也没有给出系统资源。 如何将这个整数id转换为Drawable?     

解决方法

使用
PackageManager
getResourcesForApplication()
。     ,就像CommonsWare所说的那样,您将需要使用getResourcesForApplication() 这是您可以使用它的方法,而且由于棒棒糖有一个loadIcon()函数,使用起来更容易
Drawable icon = null;
if (Build.VERSION.SDK_INT >= 21) {
    icon = info.loadIcon(this,density);
} else {
    try {
        String packageName = info.provider.getPackageName();
        Resources resources = context.getPackageManager().getResourcesForApplication(packageName);
        icon = resources.getDrawable(info.icon);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
}
    

Android AppWidget开发

Android AppWidget开发

首先,声明您的应用程序的 AndroidManifest.xml 文件中的 AppWidgetProvider 类。例如︰

<receiver android:name="ExampleAppWidgetProvider" >
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
               android:resource="@xml/example_appwidget_info" />
</receiver>

ExampleAppWidgetProvider是需要实现的APPWidgetProvider子类 Intent-filter指定了AppWidgetProvider要接收的广播事件是android.appwidget.action.APPWIDGET_UPDATE

AppWidgetProviderInfo 定义了应用程序部件,例如其最低的布局尺寸,其初始布局资源,如何经常来更新应用程序的小部件,以及 (可选) 配置活动在创建时间启动的必备素质。在使用 <appwidget-provider> 的单个元素的 XML 资源中定义的 AppWidgetProviderInfo 对象并将其保存在该项目的 res xml/文件夹。

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="40dp"
    android:minHeight="40dp"
    android:updatePeriodMillis="86400000"
    android:previewImage="@drawable/preview"
    android:initialLayout="@layout/example_appwidget"
    android:configure="com.example.android.ExampleAppWidgetConfigure" 
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen">
</appwidget-provider>

1.InitialLayout 属性指向定义的应用程序部件布局布局资源。 配置属性定义活动推出当用户添加应用程序的小部件,以便他或她要配置的应用程序部件属性。 2.PreviewImage 属性指定的应用程序部件后它配置,用户可以看到当选择的应用程序构件的外观的预览。如果未提供,用户可以相反看到您的应用程序启动器图标。此字段对应于安卓系统︰ previewImage 属性在 AndroidManifest.xml 文件中的 <receiver>元素。 3.WidgetCategory 属性声明是否可以在主屏幕 (home_screen)、 锁定屏幕 (键盘锁),或两者上显示你的应用程序部件。只有 Android 版本低于 5.0 支持锁定屏幕小部件。对于 Android 5.0 和更高,只有 home_screen 是有效的。

为应用程序窗口小部件添加边距

小部件通常不应扩展至屏幕边缘和不直观地应冲洗与其他小部件,所以你应该添加边距四面环绕你小部件框架。

到安卓 4.0 中,应用程序窗口小部件就自动给定应用程序构件与构件框架的填充边界框可提供更好的协调与其他小部件和用户的主屏幕上的图标。要利用此强烈推荐行为,请将您的应用程序的 targetSdkVersion 设置为 14 或更高版本。

创建一个布局如下面,引用其边距维度资源︰

<FrameLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:padding="@dimen/widget_margin">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@drawable/my_widget_background">
    …
  </LinearLayout>
</FrameLayout>

创建两个dimension resource,另一个在 res/value/ 中提供预安卓 4.0 自定义边距,在 res/值-v14 / 提供安卓 4.0 小部件没有额外的边距︰

res/values/dimens.xml:
<dimen name="widget_margin">8dp</dimen>
res/values-v14/dimens.xml:
<dimen name="widget_margin">0dp</dimen>

AppWidgetProvider 类作为一个方便的类来处理应用程序构件广播扩展应用。AppWidgetProvider 接收相关的应用程序部件,例如当应用程序部件是更新、 删除、 启用,和禁用的事件广播。这些广播的事件发生时,AppWidgetProvider 会收到下面的方法调用︰onUpdate()、onAppWidgetOptionsChanged()、onDeleted(Context, int[])、onEnabled(Context)、onDisabled(Context)、onReceive(Context, Intent)

最重要的 AppWidgetProvider 回调是 onUpdate(),因为它所谓每个应用程序插件添加到主界面(除非你使用一个配置活动) 时。如果您的应用程序小部件接受任何用户交互事件,那么您需要在此回调中注册事件处理程序。如果您的应用程序小部件不会创建临时文件或数据库,或者执行其他工作,需要清理,那么 onUpdate() 可能是唯一你需要定义的回调方法。例如,如果您希望应用程序构件能够启动Activity,你可以使用 AppWidgetProvider 下面的实现︰

public class ExampleAppWidgetProvider extends AppWidgetProvider {
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;
        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];
            // Create an Intent to launch ExampleActivity
            Intent intent = new Intent(context, ExampleActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            // Get the layout for the App Widget and attach an on-click listener
            // to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
            views.setOnClickPendingIntent(R.id.button, pendingIntent);
            // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
}

创建App Widget Configuration Activity 如果你想用户配置设置,当他或她将添加一个新的应用程序部件时,你可以创建应用程序构件配置活动。本次活动由应用程序构件主机也会自动启动,并允许用户配置应用程序构件可用设置在创建时,例如应用程序构件颜色、 大小、 更新期间或其他功能设置。

活动应声明为 Android 清单中的正常活动的配置文件。然而,它将推出由应用程序构件主机与 ACTION_APPWIDGET_CONFIGURE 操作,因此该活动需要接受这种意图。例如︰

<activity android:name=".ExampleAppWidgetConfigure">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
    </intent-filter>
</activity>

创建应用程序窗口小部件时,不会调用 onUpdate() 方法 (系统将不发送广播时活动启动配置的 ACTION_APPWIDGET_UPDATE)。它是配置活动首次创建应用程序窗口小部件时,从 AppWidgetManager 请求更新的责任。然而,onUpdate() 将呼吁后续更新 — — 它只跳过第一次。

Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
    mAppWidgetId = extras.getInt(
            AppWidgetManager.EXTRA_APPWIDGET_ID, 
            AppWidgetManager.INVALID_APPWIDGET_ID);
}

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.example_appwidget);
appWidgetManager.updateAppWidget(mAppWidgetId, views);

Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();

构建还有列表的应用构件 加入列表控件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <StackView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/stack_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:loopViews="true" />
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/empty_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:background="@drawable/widget_item_background"
        android:textColor="#ffffff"
        android:textandroid:text="@string/empty_view_text"
        android:textSize="20sp" />
</FrameLayout>

列表设置remoteAdapter

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
    // update each of the app widgets with the remote adapter
    for (int i = 0; i < appWidgetIds.length; ++i) {
        
        // Set up the intent that starts the StackViewService, which will
        // provide the views for this collection.
        Intent intent = new Intent(context, StackWidgetService.class);
        // Add the app widget ID to the intent extras.
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        // Instantiate the RemoteViews object for the app widget layout.
        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        // Set up the RemoteViews object to use a RemoteViews adapter. 
        // This adapter connects
        // to a RemoteViewsService  through the specified intent.
        // This is how you populate the data.
        rv.setRemoteAdapter(appWidgetIds[i], R.id.stack_view, intent);
        
        // The empty view is displayed when the collection has no items. 
        // It should be in the same layout used to instantiate the RemoteViews
        // object above.
        rv.setEmptyView(R.id.stack_view, R.id.empty_view);
        //
        // Do additional processing specific to this app widget...
        //
        
        appWidgetManager.updateAppWidget(appWidgetIds[i], rv);   
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

列表创建RemoteView

<service android:name="StackWidgetService"
...
android:permission="android.permission.BIND_REMOTEVIEWS" />
public class StackWidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
    }
}
class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
    private static final int mCount = 10;
    private List<WidgetItem> mWidgetItems = new ArrayList<WidgetItem>();
    private Context mContext;
    private int mAppWidgetId;
    public StackRemoteViewsFactory(Context context, Intent intent) {
        mContext = context;
        mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
    }
    // Initialize the data set.
        public void onCreate() {
            // In onCreate() you set up any connections / cursors to your data source. Heavy lifting,
            // for example downloading or creating content etc, should be deferred to onDataSetChanged()
            // or getViewAt(). Taking more than 20 seconds in this call will result in an ANR.
            for (int i = 0; i < mCount; i++) {
                mWidgetItems.add(new WidgetItem(i + "!"));
            }
           ...
        }
        ...
    
        // Given the position (index) of a WidgetItem in the array, use the item''s text value in 
        // combination with the app widget item XML file to construct a RemoteViews object.
        public RemoteViews getViewAt(int position) {
            // position will always range from 0 to getCount() - 1.
    
            // Construct a RemoteViews item based on the app widget item XML file, and set the
            // text based on the position.
            RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
            rv.setTextViewText(R.id.widget_item, mWidgetItems.get(position).text);
    
            // Next, set a fill-intent, which will be used to fill in the pending intent template
            // that is set on the collection view in StackWidgetProvider.
            Bundle extras = new Bundle();
            extras.putInt(StackWidgetProvider.EXTRA_ITEM, position);
            Intent fillInIntent = new Intent();
            fillInIntent.putExtras(extras);
            // Make it possible to distinguish the individual on-click
            // action of a given item
            rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);
        
            ...
        
            // Return the RemoteViews object.
            return rv;
        }
    ...
    }

我们今天的关于android – 我可以在appWidget中使用哪些视图?appwidgetsmith的分享已经告一段落,感谢您的关注,如果您想了解更多关于android app widget 中使用自定义 view、android appwidget 笔记、Android AppWidgetProviderInfo图标ID、Android AppWidget开发的相关信息,请在本站查询。

本文标签: