public class EX04_30 extends AppWidgetProvider
{
  final String addButton_actionName = "button01";
  public BluetoothAdapter blueAdapter;
  public String s;
 
  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
      int[] appWidgetIds)
  {
    // TODO Auto-generated method stub
//    Toast.makeText(context, "onUpdate", Toast.LENGTH_SHORT).show();
    // 主要的RemoteViews 
    RemoteViews mainViews = new RemoteViews(context.getPackageName(),
        R.layout.main);
    // 设定用来新增Layout的按钮Intent 
    Intent addIntent = new Intent(context, EX04_30.class);
    addIntent.setAction(addButton_actionName);
    PendingIntent addPendingIntent = PendingIntent.getBroadcast(context, 0,
        addIntent, 0);
    mainViews.setOnClickPendingIntent(R.id.Button01, addPendingIntent);

    /* 更新AppWidget */ 
    appWidgetManager.updateAppWidget(appWidgetIds, mainViews);
  }

  @Override
  public void onReceive(Context context, Intent intent)
  {
    // TODO Auto-generated method stub
//    Toast.makeText(context, intent.getAction(), Toast.LENGTH_SHORT).show();

    if (intent.getAction().equals(addButton_actionName))
    {
      // 主要的RemoteViews 
      RemoteViews mainViews = new RemoteViews(context.getPackageName(),
          R.layout.main);
      // 清空LinearLayout02里所有的View 
      //mainViews.removeAllViews(R.id.TextView01);

      blueAdapter = BluetoothAdapter.getDefaultAdapter();
      if(!blueAdapter.isEnabled()){
        blueAdapter.enable();
      }      

      mainViews.setTextViewText(R.id.Button01, "已打开蓝牙");
      // 更新AppWidget
      ComponentName thisWidget = new ComponentName(context, EX04_30.class);
      AppWidgetManager manager = AppWidgetManager.getInstance(context);
      manager.updateAppWidget(thisWidget, mainViews);
    }

    super.onReceive(context, intent);
  }
}

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()