简体   繁体   中英

Android Launcher Shortcuts

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?

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.

You can "more simply" make a hidden activity that will do what you want!

This activity must use transparent themes so it never shows and call finish() at the end of the onCreate() without any particular conditions.

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