Skip to content

Commit

Permalink
快捷方式增删改
Browse files Browse the repository at this point in the history
  • Loading branch information
huangyuanlove committed Apr 17, 2019
1 parent 9add1ee commit 45b0c2e
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 19 deletions.
5 changes: 2 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".NotificationActivity"></activity>
<activity android:name=".ShortCutActivity"></activity>
<activity android:name=".NotificationActivity" />
<activity android:name=".SAFActivity" />
<activity android:name=".TakePhotoOrChoosePhotoActivity" />


<activity android:name=".FileOperationActivity" />
<activity android:name=".MainActivity">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.saf).setOnClickListener(this);
findViewById(R.id.notification).setOnClickListener(this);
findViewById(R.id.request_permission).setOnClickListener(this);
findViewById(R.id.shortcut).setOnClickListener(this);

}

Expand Down Expand Up @@ -67,20 +68,14 @@ public void onClick(View v) {
startActivity(new Intent(MainActivity.this, NotificationActivity.class));
break;
case R.id.request_permission:


String[] permissions = new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE};

requestPermissions(permissions,REQUEST_PERMISSION_CAMERA);



// if (checkPermission(Manifest.permission.CAMERA) || Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
// Toast.makeText(MainActivity.this, "相机权限", Toast.LENGTH_SHORT).show();
// } else {
// requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_PERMISSION_CAMERA);
// }

if (checkPermission(Manifest.permission.CAMERA) || Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
Toast.makeText(MainActivity.this, "相机权限", Toast.LENGTH_SHORT).show();
} else {
requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_PERMISSION_CAMERA);
}
break;
case R.id.shortcut:
startActivity(new Intent(MainActivity.this, ShortCutActivity.class));
break;
default:

Expand All @@ -93,8 +88,8 @@ public void onClick(View v) {
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(permissions.length <= 0 || grantResults.length<=0){
return ;
if (permissions.length <= 0 || grantResults.length <= 0) {
return;
}


Expand Down
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}));
}
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,14 @@
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />

<Button
android:id="@+id/shortcut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="快捷方式"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@id/request_permission"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>
37 changes: 37 additions & 0 deletions app/src/main/res/layout/activity_short_cut.xml
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>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@

<string name="saf">SAF</string>
<string name="saf_example">文件存储框架</string>

<string name="shortcut">快捷方式</string>
<string name="shortcut_example">快捷方式</string>
</resources>

0 comments on commit 45b0c2e

Please sign in to comment.