Android Widget - 仅限第一次使用

时间:2015-02-13 11:18:10

标签: java android android-widget

我有一个小部件,在点击文本视图时,会启动一个新活动,这是一个允许用户搜索的editText。搜索后,它会以所需结果打开应用程序。如果用户将应用程序置于后台,并且没有按下,则当用户再次单击窗口小部件时,不会显示搜索editText视图,而是将应用程序打开到停止的位置。

理想情况下,我希望每次调出搜索框,即使它覆盖了应用程序背后的应用程序。有什么想法吗?代码如下:

SearchWidgetClass:

public class SearchWidget extends AppWidgetProvider {
    private static final String SHOW_POPUP_DIALOG_ACTION = "launch.search.widget";
    private static final String LAUNCH_WIDGET = "launch.homescreen.widget";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        ComponentName thisWidget = new ComponentName(context, SearchWidget.class);

        int[] allWidgetInstancesIds = AppWidgetManager.getInstance(context).getAppWidgetIds(thisWidget);
        for (int widgetId : allWidgetInstancesIds) {

            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.search_widget_layout);

            // Create an intent that when received will launch the search bar
            Intent searchIntent = new Intent(context, SearchWidget.class);
            searchIntent.setAction(SHOW_POPUP_DIALOG_ACTION);

            // Create an intent that when received will open the app on the home screen
            Intent WidgetIntent = new Intent(context, SearchWidget.class);
            WidgetIntent.setAction(LAUNCH_WIDGET);

            PendingIntent searchPendingIntent = PendingIntent.getBroadcast(context, widgetId, searchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent openPendingIntent = PendingIntent.getBroadcast(context, widgetId, WidgetIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            // Set up the onClickListener of the widget
            // Now, when the widget is pressed the pendingIntent will be sent
            remoteViews.setOnClickPendingIntent(R.id.widget_search_textview, searchPendingIntent);
            remoteViews.setOnClickPendingIntent(R.id.widget_open, openPendingIntent);

            appWidgetManager.updateAppWidget(widgetId, remoteViews);

        }//End of For Each loop

        super.onUpdate(context, appWidgetManager, appWidgetIds);

    }//End of onUpdate method

    @Override
    public void onReceive(final Context context, Intent intent) {

        Intent actionIntent;

        if (intent.getAction().equals(SHOW_POPUP_DIALOG_ACTION)) {

            actionIntent = new Intent(context, SearchWidgetDialogActivity.class);
            actionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(actionIntent);

        } else if (intent.getAction().equals(LAUNCH_WIDGET)){
            actionIntent = new Intent(context, HomeActivity.class);
            actionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(actionIntent);
        }

        super.onReceive(context, intent);

    }//End of onReceive method

}//End of SearchWidget Class

搜索对话框活动:

public class SearchWidgetDialogActivity extends Activity {

    private EditText searchWidgetEditText;
    private String actionArg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.search_widget_dialog_layout);

        LayoutParams params = getWindow().getAttributes();
        params.width = LayoutParams.MATCH_PARENT;
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);

        getWindow().setBackgroundDrawableResource(android.R.color.transparent);

        searchWidgetEditText = (EditText) findViewById(R.id.search_widget_edit_text);

        searchWidgetEditText.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if (((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) &&
                        (!StringUtils.isEmpty(searchWidgetEditText.getText().toString()))) {

                    handleWidgetAction(SearchWidgetDialogActivity.this);
                }
                return false;
            }
        });

    }//End of onCreate method

    public void handleWidgetAction(Context aContext) {

            actionArg = searchWidgetEditText.getText().toString();
            QLog.d("Launching search activity for " + actionArg + " from the widget");
            Intent serachProductsIntent = new Intent(aContext, BrowseProductsSearchActivity.class);
            serachProductsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            serachProductsIntent.setAction(Intent.ACTION_SEARCH);
            serachProductsIntent.putExtra(SearchManager.QUERY, actionArg);
            serachProductsIntent.putExtra(SearchManager.USER_QUERY, actionArg);
            aContext.startActivity(serachProductsIntent);

    }//End of handleWidgetSearch method

}// End of SearchWidgetDialogActivity class

清单活动:

<activity
            android:name="android.activities.SearchWidgetDialogActivity"
            android:label="@string/app_label"
            android:theme="@android:style/Theme.Dialog"
            android:noHistory="true"
            android:windowSoftInputMode="adjustResize">
        </activity>


<receiver 
            android:name="android.widget.SearchWidget" >
            <intent-filter>
                <action android:name="launch.search.widget"/>
                <action android:name="launch.homescreen.widget"/>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/search_widget_provider" />
        </receiver>

0 个答案:

没有答案