Home | History | Annotate | Download | only in shortcutdemo
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.example.android.pm.shortcutdemo;
     17 
     18 import android.app.IntentService;
     19 import android.content.ComponentName;
     20 import android.content.Intent;
     21 import android.content.pm.ShortcutInfo;
     22 import android.content.pm.ShortcutManager;
     23 
     24 import java.util.Arrays;
     25 
     26 /**
     27  * This allows to create a shortcut in background.
     28  *
     29  * Usage:
     30    adb shell am startservice -a com.example.android.pm.shortcutdemo.ADD \
     31      com.example.android.pm.shortcutdemo/com.example.android.pm.shortcutdemo.ShortcutPublishingService
     32  * Or for package 2,
     33    adb shell am startservice -a com.example.android.pm.shortcutdemo.ADD \
     34      com.example.android.pm.shortcutdemo2/com.example.android.pm.shortcutdemo.ShortcutPublishingService
     35 
     36  */
     37 public class ShortcutPublishingService extends IntentService {
     38     public ShortcutPublishingService() {
     39         super("ShortcutPublishingService");
     40     }
     41 
     42     @Override
     43     protected void onHandleIntent(Intent intent) {
     44         if (intent.getAction().endsWith(".ADD")) {
     45             addShortcut();
     46             return;
     47         }
     48     }
     49 
     50     private void addShortcut() {
     51         final ShortcutInfo si1 = ShortcutPublisher.addRandomIntents(
     52                 this, new ShortcutInfo.Builder(this, ("shortcut-" + System.currentTimeMillis())))
     53                 .build();
     54         ShortcutPublisher.callApi(this, () ->
     55             getSystemService(ShortcutManager.class).addDynamicShortcuts(Arrays.asList(si1)));
     56     }
     57 }
     58