views:

412

answers:

1

I have made a simple punch in / punch out time clock application. I want to add the user the option of making a shortcut on the homescreen that will toggle the state of the app(time out / time in) but I don't want this shortcut to open up the app on the screen at all.

here is my setupShortcut()

private void setupShortcut() {
    Intent shortcutIntent = new Intent(this, Toggle.class);
   // shortcutIntent.setClassName(this, Toggle.class.getName());  
    shortcutIntent.putExtra(EXTRA_KEY, "ToggleShortcut");  

    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  


    Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ToggleShortcut");
    Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this,  R.drawable.app_sample_code);
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
    setResult(RESULT_OK, intent);

}

Toggle.class is an activity that toggles the state for me. In the manifest I have these settings on it

<activity android:name=".Toggle" android:exported="true" android:theme="@android:style/Theme.Translucent.NoTitleBar">

As it is now I can create a shortcut on the home screen then press it. The first time I press it it starts the Toggle activity and completes it fine, but it also opens up the TimeClock activity on the screen. If I then hit the back button I go back to the home. I can now press this shortcut and it will start the Toggle activity and not change the screen. Before I added: shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); It would open up the TimeClock activity every time. Now it is just the first time. But I want it to never show anything on the screen when the Toggle shortcut is pressed. Does anyone have any idea of how to get rid of that?

+3  A: 

The problem with launchers is that they start Activities, whereas what you want is to start a Service so you can modify the app state in the background. It sounds like what you really want to do is make a Widget, not a launcher shortcut. You can configure the widget to have a button that sets off a Service that will toggle things in the background.

Daniel Lew
You can also just call finish() in the Activity's onCreate() method, the Activity won't show up. A service would be overkill for this.
Romain Guy
I am calling finish() in the Toggle Activity. I also never call setContentView(); This works for all but the first time I push the shortcut. I put a Log output in the onCreate() of the TimeClock activity and found out that it is not being called when the shortcut is pushed. It is simply taking an already open instance of the activity and showing it. I also tried making the setupShortcut() intent go to "com.timeclock.TOGGLE" I put this as an intent filter on my TimeClock activity and put an if statement to check the action on the incoming intent.
Tim
This if statement is before setContentView. And it calls finish() but I still get a black screen flash when I push the shortcut.
Tim