您的位置:首页 > 移动开发

使用AppWidget跳转Activity

2013-09-06 21:49 148 查看


 




 


 

 

 

 

AppWidget
  使用AppWidget跳转到Activity
  Appwidget就是将一些组件设置到桌面上显示。
 
android.appwidget包中定义的类:
1、        AppWidgetProvider:定义了AppWidget的基本操作,需要通过子类进行设置;
2、        AppWidgetProviderInfo:AppWidget组件的元数据提供者,例如:组件的大小、更新的时间等;
3、        AppWidgetHostView:创建AppWidget的View显示,些为真正的View,与之对应的还有一个RemoteView;
4、        AppWidgetHost:监听AppWidget的服务以及创建AppWidgetHostView
5、        AppWidgetManager:用于更新相应的AppWidget。
 
 
 
 
将本程序安装后,按Home键回到手机桌面,长按桌面后出现上下文对话框,此时选择“窗口小部件”,接着找到我们安装的程序,
我这里的程序名称为“桌面显示组件”,这样在我们的手机桌面上就出现了程序的快捷方式,以后我们只要一按此快捷方式
就能启动我们的Activity了。
 

 

 

在res/drawable-hdpi下加入一张名为picture和一张名为button的图片以供使用。
 
 
 
定义一个继承AppWidgetProvider的类MyAppWidget,代码如下:
 
package com.li.appwidget;
 
import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
 
@SuppressLint("ParserError")
public class MyAppWidget extends AppWidgetProvider {
  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
       int[] appWidgetIds) {
     for(int x = 0; x < appWidgetIds.length;x++){
       Intent intent = new Intent(context,MyAppWidgetDemo.class);
       PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
       RemoteViews remote = new RemoteViews(context.getPackageName(),
            R.layout.liyewen_appwidget);
       remote.setOnClickPendingIntent(R.id.but, pendingIntent);
       appWidgetManager.updateAppWidget(appWidgetIds[x], remote);
     }
  }
}
 
package com.li.appwidget;
 
import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
 
@SuppressLint("ParserError")
public class MyAppWidget extends AppWidgetProvider {
  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
       int[] appWidgetIds) {
     for(int x = 0; x < appWidgetIds.length;x++){
       Intent intent = new Intent(context,MyAppWidgetDemo.class);
       PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
       RemoteViews remote = new RemoteViews(context.getPackageName(),
            R.layout.liyewen_appwidget);
       remote.setOnClickPendingIntent(R.id.but, pendingIntent);
       appWidgetManager.updateAppWidget(appWidgetIds[x], remote);
     }
  }
}
 
 
 
 
 
 
在res下新建xml文件夹,并在xml下新建一个资源配置文件liyewen_appwidget.xml,
此文件用于读取AppWidgetProvider的配置信息,代码如下:
 
<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:minHeight="80dp"
    android:minWidth="300dp"
    android:updatePeriodMillis="6000"
    android:initialLayout="@layout/liyewen_appwidget">
 
</appwidget-provider>
 
 
 
 
 
在layout文件夹中新建布局文件liyewen_appwidget.xml
此布局文件用于以后桌面组件的显示,代码如下:
 
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal">
  <Button
      android:id="@+id/but"
      android:layout_width="160dp"
      android:layout_height="40dp"
      android:text="按我启动Activity"
      android:background="@drawable/button"
      android:textColor="#ffffff"/>
</LinearLayout>
 
 
 
主Activity(MyAppWidgetDemo.java)的代码如下:
 
 
package com.li.appwidget;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
 
public class MyAppWidgetDemo extends Activity {
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
 
}
 
 
 
 
main.xml布局文件的代码如下:
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_marginTop="16dp"
        android:background="@drawable/picture"
        android:text="这是启动Activity后的界面"
        android:textColor="#ffff00"/>
 
</LinearLayout>
 
 
 
 
 
 
修改AndroidManifest.xml文件,将之前在res/xml文件夹中配置的文件定义在广播之中。
 
增加的部分为:
 
<receiver android:name=".MyAppWidget">
            <intent-filter >
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/liyewen_appwidget"/>
        </receiver>
 
 
整个AndroidManifest.xml文件为(我用的SDK是最新的,所以AndroidManifest.xml文件会有些不同):
 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.li.appwidget"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MyAppWidgetDemo"
            android:label="@string/title_activity_my_app_widget_demo" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".MyAppWidget">
            <intent-filter >
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/liyewen_appwidget"/>
        </receiver>
    </application>
</manifest>
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息