android开发之APP Widget
android开发之APP Widget
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public 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;
}
}
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实例被删除时,会调用
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
System.out.println("onDeleted()");
}
//当最后一个app widget实例被删除时,会调用
@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
super.onDisabled(context);
System.out.println("onDisabled()");
}
//当第一个App widget实例被创建时,会调用
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
System.out.println("onEnabled()");
}
//接收app收到的所有广播事件
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.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控件的text
remoteView.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实例时,会调用此方法
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
System.out.println("onUpdate()");
//为每一个app widget实例绑定一个remoteViews监听器,每创建的一个app widget实例都有一个对应的ID
for(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打包给pendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
*/
//end part1
//part 2:pendingIntent用来发送一个广播
//设置action
intent.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获得intent
remoteView.setOnClickPendingIntent(R.id.WidgetButton, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteView);
}
}
}
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
<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
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>
<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
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.AppWidgetProviderTest">
<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>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。