-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9add1ee
commit 45b0c2e
Showing
6 changed files
with
157 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
app/src/main/java/com/huangyuanlove/adaptationhighversion/ShortCutActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.huangyuanlove.adaptationhighversion; | ||
|
||
import android.content.Intent; | ||
import android.content.pm.ShortcutInfo; | ||
import android.content.pm.ShortcutManager; | ||
import android.graphics.drawable.Icon; | ||
import android.os.Build; | ||
import android.support.annotation.RequiresApi; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.Toast; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class ShortCutActivity extends AppCompatActivity implements View.OnClickListener { | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_short_cut); | ||
findViewById(R.id.add_shortcut).setOnClickListener(this); | ||
findViewById(R.id.remove_shortcut).setOnClickListener(this); | ||
findViewById(R.id.update_shortcut).setOnClickListener(this); | ||
} | ||
|
||
@Override | ||
public void onClick(View v) { | ||
switch (v.getId()) { | ||
case R.id.add_shortcut: | ||
addShortcut(); | ||
break; | ||
case R.id.remove_shortcut: | ||
removeShortcut(); | ||
break; | ||
case R.id.update_shortcut: | ||
updateShortcut(); | ||
break; | ||
} | ||
} | ||
|
||
private void updateShortcut() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { | ||
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); | ||
Intent intent = new Intent(ShortCutActivity.this,ShortCutActivity.class); | ||
intent.setAction(Intent.ACTION_VIEW); | ||
ShortcutInfo shortcut = new ShortcutInfo.Builder(ShortCutActivity.this, "shortcut") | ||
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)) | ||
.setShortLabel( getString( R.string.shortcut) +"new") | ||
.setLongLabel(getString(R.string.shortcut_example) +"new") | ||
.setIntent(intent) | ||
.build(); | ||
shortcutManager.updateShortcuts(Arrays.asList( new ShortcutInfo[]{ shortcut})); | ||
} | ||
} | ||
|
||
private void removeShortcut() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { | ||
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); | ||
|
||
//动态添加并且在桌面创建了快捷方式,删除之前,先disable一下. 或者判断 | ||
List<ShortcutInfo> shortcutInfos = shortcutManager.getPinnedShortcuts(); | ||
|
||
|
||
for(ShortcutInfo si : shortcutInfos){ | ||
if(si.getId().equals("shortcut")){ | ||
shortcutManager.disableShortcuts(Arrays.asList(new String[]{"shortcut"})); | ||
} | ||
} | ||
shortcutManager.removeDynamicShortcuts(Arrays.asList(new String[]{"shortcut"})); | ||
|
||
Toast.makeText(ShortCutActivity.this,"共" + shortcutInfos.size() +"个桌面快捷方式",Toast.LENGTH_SHORT).show(); | ||
|
||
|
||
} | ||
} | ||
|
||
private void addShortcut() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { | ||
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); | ||
Intent intent = new Intent(ShortCutActivity.this,ShortCutActivity.class); | ||
intent.setAction(Intent.ACTION_VIEW); | ||
ShortcutInfo shortcut = new ShortcutInfo.Builder(ShortCutActivity.this, "shortcut") | ||
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)) | ||
.setShortLabel( getString( R.string.shortcut)) | ||
.setLongLabel(getString(R.string.shortcut_example)) | ||
.setIntent(intent) | ||
.build(); | ||
shortcutManager.addDynamicShortcuts(Arrays.asList( new ShortcutInfo[]{ shortcut})); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".ShortCutActivity"> | ||
|
||
<Button | ||
android:id="@+id/add_shortcut" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="添加快捷方式" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintLeft_toLeftOf="parent" | ||
app:layout_constraintRight_toRightOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
<Button | ||
android:id="@+id/remove_shortcut" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="删除快捷方式" | ||
app:layout_constraintTop_toBottomOf="@id/add_shortcut" | ||
app:layout_constraintLeft_toLeftOf="parent" | ||
app:layout_constraintRight_toRightOf="parent"/> | ||
|
||
<Button | ||
android:id="@+id/update_shortcut" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="更新快捷方式" | ||
app:layout_constraintTop_toBottomOf="@id/remove_shortcut" | ||
app:layout_constraintLeft_toLeftOf="parent" | ||
app:layout_constraintRight_toRightOf="parent"/> | ||
|
||
</android.support.constraint.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters