android widget-demo

public class WidgetDemo extends AppWidgetProvider {
Context context;
AppWidgetManager appWidgetManager;
ComponentName cn;
RemoteViews rv;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
/*Timer timer=new Timer();
timer.scheduleAtFixedRate(new MyTimer(context, appWidgetManager), 0, 1000);*/
rv=new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
cn=new ComponentName(context, WidgetDemo.class);
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss");
   rv.setTextViewText(R.id.tv, "当前时间:"+sdf.format(new Date()));
   rv.setImageViewResource(R.id.iv, R.drawable.huaixiao);
   Intent intent = new Intent("android.appwidget.action.APPWIDGET_UPDATE");
   intent.setClass(context,TestActivity.class);
   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   Bundle bundle=new Bundle();
   bundle.putString("name", "hello widget!!!");
   intent.putExtras(bundle);
   PendingIntent pendingIntent=PendingIntent.getActivity(context, 0, intent, 0);
   rv.setOnClickPendingIntent(R.id.iv, pendingIntent);
   appWidgetManager.updateAppWidget(cn, rv);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("android.appwidget.action.APPWIDGET_UPDATE")){
Toast.makeText(context, "hello!!!", Toast.LENGTH_SHORT).show();
}
System.out.println("OnReceive!!");
super.onReceive(context, intent);
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
System.out.println("OnDeleted!!");
super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
System.out.println("OnDisabled!!");
super.onDisabled(context);
}
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
System.out.println("OnEnabled!!");
super.onEnabled(context);
}

/*class MyTimer extends TimerTask{
Context context;
AppWidgetManager appWidgetManager;
ComponentName cn;
RemoteViews rv;
@Override
public void run() {
// TODO Auto-generated method stub
rv=new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss");
   rv.setTextViewText(R.id.tv, "当前时间:"+sdf.format(new Date()));
   rv.setImageViewResource(R.id.iv, R.drawable.huaixiao);
   appWidgetManager.updateAppWidget(cn, rv);
}

public MyTimer(Context context, AppWidgetManager appWidgetManager){
this.context=context;
this.appWidgetManager=appWidgetManager;
cn=new ComponentName(context, WidgetDemo.class);
}

}*/



}




public class TestActivity extends Activity {
String name="";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button=(Button)findViewById(R.id.button);
        EditText et=(EditText)findViewById(R.id.et);
        Bundle bundle=this.getIntent().getExtras();
        try{
        if(bundle.getString("name")!=null){
                 name=bundle.getString("name");
                 et.setText(name);
             }
        }catch(Exception e){
        e.printStackTrace();
        }
      
        button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(TestActivity.this, name, Toast.LENGTH_SHORT).show();
}
});
    }
}


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.gys.widget"
      android:versionCode="1"
      android:versionName="1.0">




    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TestActivity"
                  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=".WidgetDemo"
 android:icon="@drawable/huaixiao"
                  android:label="@string/widget">     
            <intent-filter>     
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
            </intent-filter>     
            <meta-data android:name="android.appwidget.provider"
                       android:resource="@xml/widget"    
            />     
        </receiver>
    </application>
</manifest>


<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:minWidth="124dip"
  android:minHeight="50dip"
  android:updatePeriodMillis="180001"
  android:initialLayout="@layout/widgetlayout"
  />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值