package com.mymusictwo;
import android.annotation.SuppressLint;
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;
import com.service.MusicService;
@SuppressLint("HandlerLeak")
public class MusicWidget extends AppWidgetProvider
{
public RemoteViews views;//RemoteView对象
public ComponentName thisWidget; //组件名
public AppWidgetManager manager; // AppWidget管
//时间监控
Context context;
//Intent intentOne;
public void onEnabled(Context context) {
// TODO Auto-generated method stub
// widget第一次创建的时候 执行的方法
// 初始化widget数据的操作,开启以后后台
super.onEnabled(context);
this.context = context;
context.startService(new Intent(context, MusicService.class));
}
@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
super.onDisabled(context);
context.stopService(new Intent(context, MusicService.class));
//System.out.println("onDisabled");
// 当所有的widget都被删除的时候 执行 ondisable();
// 停止我们开启的服务
// 删除垃圾文件 临时文件
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds)
{
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++)
{
int appWidgetId = appWidgetIds[i];
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.music_widget);
Intent prevInten=new Intent("click");
PendingIntent Pprevintent = PendingIntent.getBroadcast(context, 0,prevInten, 0);
remoteViews.setOnClickPendingIntent(R.id.imageButton1,Pprevintent);
Intent prevIntenOne=new Intent("virbrate");
PendingIntent PprevintentOne = PendingIntent.getBroadcast(context, 0,prevIntenOne, 0);
remoteViews.setOnClickPendingIntent(R.id.virbrate,PprevintentOne);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
}
|
|