以编程方式绕过"触摸以配置" Android Widget

时间:2016-08-08 14:43:25

标签: java android xml initialization config

我想知道如何绕过"触摸配置"在我的android小部件上以编程方式。一旦我在我的设备上安装了Android小部件,它就会显示"触摸以配置"然后执行我的课程" IntelligenWidgetConfig.java" 。但我想在启动我的应用程序时自动发生这种情况。任何人都可以告诉我如何处理这种方法或任何可能有帮助的解决方案吗?

Android配置类:

public class IntelliGenWidgetConfig extends Activity {
    private static final String TAG = "IntelliGenWidgetConfig";

    public IntelliGenWidgetConfig() {
        super();
    }

    public static int getAppWidgetId(Context context) {
        SharedPreferences settings = context.getSharedPreferences("Intelligen", MODE_PRIVATE);
        return settings.getInt("appWidgetId", 1);
    }

    protected void setAppWidgetId(int id) {
        SharedPreferences settings = getSharedPreferences("Intelligen", MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt("appWidgetId", id);
        editor.commit();
    }

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

        // Set the result to CANCELED.  This will cause the widget host to cancel
        // out of the widget placement if they press the back button.
        setResult(RESULT_CANCELED);

        // Find the widget id from the intent.
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            setAppWidgetId(extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID));
        }

        // If they gave us an intent without the widget id, just bail.
        if (getAppWidgetId(this) == AppWidgetManager.INVALID_APPWIDGET_ID) {
            finish();
        }

        final Context context = IntelliGenWidgetConfig.this;

        RegisterForPushNotification(context);

        // Push widget update
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.intelligen_widget_layout);
        appWidgetManager.updateAppWidget(getAppWidgetId(this), views);

        // Make sure we pass back the original appWidgetId
        Intent resultValue = new Intent();
        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, getAppWidgetId(this));
        setResult(RESULT_OK, resultValue);
        finish();
    }

    private void RegisterForPushNotification(Context ctx) {
        Intent registerIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
        registerIntent.putExtra("app", PendingIntent.getBroadcast(ctx, 0, new Intent(), 0));
        registerIntent.putExtra("sender", "209845001369");
        startService(registerIntent);
    }

    private void DeregisterFromPushNotification(Context ctx) {
        Intent unregisterIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
        unregisterIntent.putExtra("app", PendingIntent.getBroadcast(ctx, 0, new Intent(), 0));
        startService(unregisterIntent);
    }
}

AndroidManifest.xml(部分):

   <activity android:name=".IntelliGenWidgetConfig" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
        </intent-filter>
    </activity>

XML

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="180dp"
    android:minHeight="360dp"
    android:updatePeriodMillis="0"
    android:initialLayout="@layout/intelligen_widget_layout"
    android:resizeMode="vertical"
    android:minResizeWidth="280dp"
    android:minResizeHeight="70dp"
    android:configure="za.co.infotech.www.intelligen.IntelliGenWidgetConfig"
    android:previewImage="@drawable/preview">
</appwidget-provider>

0 个答案:

没有答案