android开发-app widget

来源:互联网 发布:解压包软件 编辑:程序博客网 时间:2024/05/17 03:24

本博文主要讲述的android开发中的桌面程序的开发--APP widget,主要用的是PendingIntent和RemoteViews。 PendingIntent主要用来设置桌面程序的相应方式。此对象可以有三种Intent方式,第一个是开始一个新的Activity,第二个是发送一个广播,第三个是开始一个service。 

RemoteViews的作用:因为桌面程序和app程序不是属于一个进程,不能通过正常的操作控件的方式来操作。需要使用系统提供的RemoteViews对象来操作桌面程序。在这个类中有设置APP Widget中控件属性的方法。 
下面我们来看看代码,在代码中有分别包含了上述所说的PendingInetnt的两种方式(part 1:开始一个新的Activity;part 2:发送一个广播)和RemoteViews的使用方法: 

主程序的Activity,MainActivity.java: 

package com.example.appwidget;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}appwidget桌面程序的类AppWidgetProviderTest.java:package com.example.appwidget;import android.app.PendingIntent;import android.appwidget.AppWidgetManager;import android.appwidget.AppWidgetProvider;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.widget.RemoteViews;public class AppWidgetProviderTest extends AppWidgetProvider {//自定义一个ACTION常量private final String USER_ACTION = "com.example.appwiget.USER_ACTION";//当APP widget实例被删除时,会调用@Overridepublic void onDeleted(Context context, int[] appWidgetIds) {// TODO Auto-generated method stubsuper.onDeleted(context, appWidgetIds);System.out.println("onDeleted()");}//当最后一个app widget实例被删除时,会调用@Overridepublic void onDisabled(Context context) {// TODO Auto-generated method stubsuper.onDisabled(context);System.out.println("onDisabled()");}//当第一个App widget实例被创建时,会调用@Overridepublic void onEnabled(Context context) {// TODO Auto-generated method stubsuper.onEnabled(context);System.out.println("onEnabled()");}//接收app收到的所有广播事件@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubSystem.out.println("onReceive()");//比较下,如果收到的广播是USER_ACTION,则输出信息,且更新桌面控件if(USER_ACTION.equals(intent.getAction())){System.out.println("onReceive --> " + USER_ACTION);//获得RemoteViews对象RemoteViews remoteView = new RemoteViews(context.getPackageName(),R.layout.appwidget_provider);//更新桌面TextView控件的textremoteView.setTextViewText(R.id.myText, "onReceive");AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);ComponentName componentName = new ComponentName(context, AppWidgetProviderTest.class);appWidgetManager.updateAppWidget(componentName, remoteView);}else{//调用父类的方法super.onReceive(context, intent);}}//在到达指定的更新时间之后或者向桌面添加App widget实例时,会调用此方法@Overridepublic void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {// TODO Auto-generated method stubsuper.onUpdate(context, appWidgetManager, appWidgetIds);System.out.println("onUpdate()");//为每一个app widget实例绑定一个remoteViews监听器,每创建的一个app widget实例都有一个对应的IDfor(int i = 0; i < appWidgetIds.length; i ++){System.out.println("appWidgetIds -->" + appWidgetIds[i]);//创建一个Intent对象Intent intent = new Intent();//part 1:pendingIntent用来打开一个新的Activity/*intent.setClass(context, MainActivity.class);//使用getActivity方法创建一个pendingIntent对象,此对象用于跳转Activity,将intent打包给pendingIntentPendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);*///end part1//part 2:pendingIntent用来发送一个广播//设置actionintent.setAction(USER_ACTION);//使用getBroadcast()方法创建一个pendingIntent对象PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);//end part2//创建一个RemoteViews对象RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider);//RemoteViews对象将Button和pendingIntent绑定。通过pendingIntent获得intentremoteView.setOnClickPendingIntent(R.id.WidgetButton, pendingIntent);appWidgetManager.updateAppWidget(appWidgetIds[i], remoteView);}}}
app widget的界面布局文件appwidget_provider.xml:(在桌面看到的界面)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >   <TextView        android:id="@+id/myText"       android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:text="welcome appwidget"       android:background="#ff0000"       />   <Button       android:id="@+id/WidgetButton"       android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:text="Widget Button"       /></LinearLayout>
appwidget_provider的元数据的文件appwidget_provider_info.xml:

<?xml version="1.0" encoding="utf-8"?><appwidget-provider    xmlns:android="http://schemas.android.com/apk/res/android"    android:minWidth="180dp"    android:minHeight="40dp"    android:updatePeriodMillis="1000"    android:initialLayout="@layout/appwidget_provider"     ></appwidget-provider>
AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.appwidget"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity<br>            android:name="com.example.appwidget.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <receiver            android:name="com.example.appwidget>            <intent-filter>                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>            </intent-filter>            <intent-filter ><!--自定义的过滤器  -->                <action android:name="com.example.appwiget.USER_ACTION"/>            </intent-filter>            <meta-data                 android:name="android.appwidget.provider"                android:resource="@xml/appwidget_provider_info"/>        </receiver>    </application></manifest>
app widget需要在桌面上添加我们创建的app,才会在桌面上显示出来。可以空白处长按,就会出来添加widget选项,选中我们的app既会在桌面显示。

转载:http://www.2cto.com/kf/201412/360947.html

0 0
原创粉丝点击