2012-03-11 71 views
0

我想創建一個小部件,當點擊打開一個對話框,一個autocompletetextview(來自main.class)和mainclass執行功能..這是我的小部件類,請告訴我應該穿什麼在Android清單也。 THX小部件自動完成的TextView

public class AppWidget extends AppWidgetProvider 
{ 
@Override 
public void onReceive(Context ctxt, Intent intent) 
{ 
    if(intent.getAction()==null) 
    { 
     ctxt.startService(new Intent(ctxt,ToggleService.class)); 
    } 
    else 
    { 
     super.onReceive(ctxt, intent); 
    } 

} 

@Override 
public void onUpdate(Context context,AppWidgetManager appWidgetManager, int [] appWidgetIds) 
{ 
    context.startService(new Intent(context,ToggleService.class)); 
    //RemoteViews buildUpdate(context); 
} 

public static class ToggleService extends IntentService 
    { 
    public ToggleService() { 
     super("AppWidget$ToggleService"); 

     } 



@Override 
protected void onHandleIntent(Intent intent) 
{ 
    ComponentName me = new ComponentName(this,AppWidget.class); 
    AppWidgetManager mgr= AppWidgetManager.getInstance(this); 
    mgr.updateAppWidget(me,buildUpdate(this)); 
} 

private RemoteViews buildUpdate(Context context) 
{ 
    RemoteViews updateViews=new RemoteViews(context.getPackageName(),R.layout.widget); 


    Intent i=new Intent(this, AppWidget.class); 
    PendingIntent pi= PendingIntent.getBroadcast(context,0, i,0); 
    updateViews.setOnClickPendingIntent(R.id.phoneState,pi); 
    return updateViews; 
} 

} }在res

widgetxml //

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<ImageView 

    android:id="@+id/phoneState" 

    android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:layout_centerInParent="true" 
     android:src="@drawable/ic_launcher" 
    /> 



</RelativeLayout> 

//和widget_provider.xml/XML

 <?xml version="1.0" encoding="utf-8"?> 
    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 

    android:minWidth="79px" 
    android:minHeight="79px" 
    android:updatePeriodMillis="1800000" 
    android:initialLayout="@layout/widget"> 


<TextView 

android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Loading..." /> 

//和部分從我的清單

<receiver android:name=".AppWidget" 
     android:label="Caller" 
     android:icon="@drawable/ic_launcher" > 
     <intent-filter > 
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 

     </intent-filter> 
     <meta-data 
      android:name="android.appwidget.provider" 
      android:resource="@xml/widget_provider" 
      /> 

    </receiver> 
    <service android:name=".AppWidget$ToggleService" /> 
+1

你的標題是這麼久......像「不使用關鍵字的任何人都可以referm我一些示例我應該怎麼做「 – 2012-03-11 18:28:19

+0

嗨託尼,這是imran。你想開始一個服務或活動? – 2012-03-11 18:51:40

+0

當我按下我想用我的autocompletetextview(其搜索聯繫人和當點擊調用它們),反正這是可能的,當點擊小工具來顯示自動完成(因爲autocomplete..cant是一個小部件),並幫助我的小工具正如我告訴你對話框不小部件點擊強制執行開放執行該自動完成在Main.class執行命令...... THX – Tony 2012-03-11 18:59:36

回答

1

UPADTE您的manifest.xml

<receiver android:name=".AppWidget" 
     android:label="Caller" 
     android:icon="@drawable/ic_launcher" > 
     <intent-filter > 
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
<action android:name="com.app.example.MyWidget.ACTION_WIDGET_CLICK_RECEIVER"/> 

     </intent-filter> 
     <meta-data 
      android:name="android.appwidget.provider" 
      android:resource="@xml/widget_provider" 
      /> 

    </receiver> 
    <service android:name=".AppWidget$ToggleService" /> 

和更新您的AppWidgetProvider

public class MyWidget extends AppWidgetProvider { 
     public static String ACTION_WIDGET_CLICK_RECEIVER = "ActionReceiverWidget"; 

     public static int appid[]; 
     public static RemoteViews rview; 
     @Override 
     public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
       int[] appWidgetIds){ 
      updateWidgetState(context, ""); 
     } 
     @Override 
     public void onReceive(Context paramContext, Intent paramIntent) 
      { 
      String str = paramIntent.getAction(); 
      if (paramIntent.getAction().equals(ACTION_WIDGET_CLICK_RECEIVER)) { 
       updateWidgetState(paramContext, str); 
      } 
      else 
      { 
        if ("android.appwidget.action.APPWIDGET_DELETED".equals(str)) 
         { 
         int i = paramIntent.getExtras().getInt("appWidgetId", 0); 
         if (i == 0) 
         { 

         } 
         else 
         { 
          int[] arrayOfInt = new int[1]; 
          arrayOfInt[0] = i; 
          onDeleted(paramContext, arrayOfInt); 
         } 
         } 
       super.onReceive(paramContext, paramIntent); 
      } 
      } 
     static void updateWidgetState(Context paramContext, String paramString) 
      { 
      RemoteViews localRemoteViews = buildUpdate(paramContext, paramString); 
      ComponentName localComponentName = new ComponentName(paramContext, MyWidget.class); 
      AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, localRemoteViews); 
      } 
     private static RemoteViews buildUpdate(Context paramContext, String paramString) 
      { 
      // Toast.makeText(paramContext, "buildUpdate() ::"+paramString, Toast.LENGTH_SHORT).show(); 
      rview = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layout); 
      Intent active = new Intent(paramContext, MyWidget.class); 
      active.setAction(ACTION_WIDGET_RECEIVER); 
      active.putExtra("msg", "Message for Button 1"); 
      PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext, 0, active, 0); 
      rmViews.setOnClickPendingIntent(R.id.buttonus1, configPendingIntent); 
      if(parmString.equals(ACTION_WIDGET_CLICK_RECEIVER)) 
      { 

       //open a dialog with a autocompletetextview 
      //your code for update and what you want on button click 

      } 
      return rview; 
      } 
     @Override 
     public void onEnabled(Context context){ 
      super.onEnabled(context); 
      // Toast.makeText(context, "onEnabled() ", Toast.LENGTH_SHORT).show(); 
     } 
     // Called each time an instance of the App Widget is removed from the host 
     @Override 
     public void onDeleted(Context context, int [] appWidgetId){ 
      super.onDeleted(context, appWidgetId); 
      // Toast.makeText(context, "onDeleted() ", Toast.LENGTH_SHORT).show(); 
     } 
     // Called when last instance of App Widget is deleted from the App Widget host. 
     @Override 
     public void onDisabled(Context context) { 
      super.onDisabled(context); 
      // Toast.makeText(context, "onDisabled() ", Toast.LENGTH_SHORT).show(); 
     } 

    } 
+1

但昨天託尼。 – 2012-03-11 19:06:34

+0

,因爲我的名字PGK在清單是dd.caller我應該寫 <操作機器人:名字=「dd.caller.MyWidget.ACTION_WIDGET_CLICK_RECEIVER」 /> ?? – Tony 2012-03-11 19:07:09

+0

我只需要在的AppWidgetProvider代碼打開一個對話框,我會寫全部倒在對話框中,如果對話框是透明BG活動沒關係只是如何通過小部件 – Tony 2012-03-11 19:09:18