android应用内部实现生成桌面快捷方式与进度条控制音量大小

来源:互联网 发布:windows优化大师64位 编辑:程序博客网 时间:2024/06/10 04:15

最近日子总算有点样子了,趁空闲将自己最近工作中用到的知识总结一下分享给大家。第一个就是在应用中加入可以在桌面上生成快键方式的功能,比如聊天APP在桌面生成某个经常联系的好友的快捷方式,在桌面点击快捷方式可以直接进入与他的聊天页面。第二个就是使用进度条去控制音量的大小。这些都是自己用过的下面会粘一些代码,比较急的童鞋可以直接使用。好了,看代码吧。


桌面生成快捷方式

1.在清单文件注册receiver。

       <receiver            android:name="com.android.launcher2.InstallShortcutReceiver"            android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">            <intent-filter>                <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />            </intent-filter>        </receiver>

2.添加权限。

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

3.在需要生成快捷方式的地方添加如下代码,注释很清楚,大家只用根据需求修改相关内容就可以。

 public void createShortCut(){                    //创建快捷方式的Intent                    Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");                    //不允许重复创建                    shortcutintent.putExtra("duplicate", false);                    //需要现实的名称也就是桌面快捷方式的名字                    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));                    //快捷方式的图片                    Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.bz);                    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);                    //点击快捷图片,运行的程序入口                    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , TestMusicActivity.class));                    //发送广播。OK                    sendBroadcast(shortcutintent);    }


使用进度条控制音量的大小

1.声明相关控件以及音量的管理者AudioManager,此处省略。

2.得到音量管理者,获得音量的最大值以及当前音量大小设置为进度条的最大值以及初始值。添加进度条的监听。

audioManager=(AudioManager)getSystemService(AUDIO_SERVICE);int MaxSound=audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);switch_sound.setMax(MaxSound);int currentSount=audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);switch_sound.setProgress(currentSount);switch_sound.setOnSeekBarChangeListener(new SeekBarListener());
class SeekBarListener implements SeekBar.OnSeekBarChangeListener {    @Override    public void onProgressChanged(SeekBar seekBar, int progress,                                  boolean fromUser) {        if (fromUser) {            int SeekPosition=seekBar.getProgress();            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, SeekPosition, 0);        }    }    @Override    public void onStartTrackingTouch(SeekBar seekBar) {    }    @Override    public void onStopTrackingTouch(SeekBar seekBar) {    }}

  坐等下班,祝大家都开心。

0 0