简体   繁体   中英

Launch an Activity on the first launch of a widget

I would like to launch an Activity, when the user adds the widget on the launcher. How can I do that?

The onReceive method is called too often. And with onEnabled, it simply doesn't launch.

How can I do that?

Tkx

Widget Doesn't have a OnCreate() method. Instead it has a onEnabled() Method.

    @Override
    public void onEnabled (Context context){
    super.onEnabled(context);

    Toast.makeText(context, "Launching Config Activity", Toast.LENGTH_SHORT).show();

    //Launching the Widget Config Activity on creating widget first time
    myIntent = new Intent(context, ConfigActivity.class);
    //Needed because activity is launched from outside another activity
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    myIntent.putExtra("WIDGET_SIZE", "default");
    context.startActivity(myIntent);
    }

Remember you need to add the widget to the home screen using code after the configuration is complete with necessary changes.

Refer more here: http://developer.android.com/guide/topics/appwidgets/index.html

I'm not sure on this, I havne't done a widget yet, but I think when you create a widget, the widgets onCreate() method gets called. Try placing your startActivity(Intent) in there and see if that works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM