一、显示壁纸

显示壁纸也是launcher必不可少的功能,下面我们看看如何让我们开发的launcher来显示壁纸。

要在我们的activity里显示一个壁纸非常简单(包括动态壁纸也如此),我们只需要定义一个theme使其继承自android:Theme.Wallpaper,然后在activity中使用这个theme就ok了。
在res/valuse下面增加一个xml文件,其名称为styles.xml(AndroidStudio新建的项目会自动创建styles.xml我们只需要在resources标记对下添加),内容如下:

<style name="SAOTheme" parent="android:Theme.Wallpaper">        <!-- windowNoTitle设置为true,去掉标题栏 -->        <item name="android:windowNoTitle">true</item></style>

此时整个工程的结果如下:



下面在AndroidManifest.xml中使用这个theme,如下图所示:



xml代码如下
?

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.sljjyy.sao.launcher"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="7"        android:targetSdkVersion="16" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.sljjyy.sao.launcher.MainActivity"            android:theme="@style/SAOTheme"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />                <category android:name="android.intent.category.HOME" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>        </activity>    </application></manifest>


好了,运行程序,可以看到壁纸的显示效果了:



二、设置壁纸

用代码设置壁纸也是非常地简单的事,我们只需要向系统发送一个“设置请求”就足够了,其它的事情系统处理。

用下面代码表示:

 public void onSetWallpaper(View view) {        //生成一个设置壁纸的请求        final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);        Intent chooser = Intent.createChooser(pickWallpaper,"chooser_wallpaper");        //发送设置壁纸的请求        startActivity(chooser);    }

原教程中是在新建项目中加Button实现的,但是由于审美的关系,不希望加个很丑的Button在界面上,然后就发现了AndroidStudio已经贴心的预设了Menu,不过在搞了很久才搞定的

res/menu/main.xml 直接可以使用

<menu xmlns:android="http://schemas.android.com/apk/res/android">    <item android:id="@+id/action_settings"        android:title="settings"        android:orderInCategory="100"        android:showAsAction="never" /></menu>

?

Activity上也已经做好了关联

    @Override        public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }

我们只需要设定好动作 调用
onSetWallpaper()方法就可以了

 public boolean onOptionsItemSelected(MenuItem item){        switch (item.getItemId()) {            case R.id.action_settings:                onSetWallpaper();                return true;        }        return false;    }


android手把手教你开发launcher(一)(AndroidStudio版)

android手把手教你开发launcher(二)——列出安装的应用程序

android手把手教你开发launcher(三)——启动安装的应用程序

android手把手教你开发launcher(四)——显示widget

android手把手教你开发launcher(五)——设置壁纸



转自:http://www.bangchui.org/read.php?tid=12386

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. 第四课:android数据相关---sharedPreferences
  3. 为Android添加底层核心服务
  4. Android将应用程序指定默认语言
  5. Android(安卓)APK反编译方法(可以获取APK xml和android Manifest,j
  6. Android(安卓)沉寖状态栏与透明状态栏
  7. Android系统架构图及简单的系统架构
  8. Android入门进阶教程(16)-ActivityThead、ActivityManagerServic
  9. Android中Service和Activity相互通信示例代码

随机推荐

  1. Android4: Write Storage权限问题
  2. android之Activity的Tasks and Back Stac
  3. android 数据传输
  4. Android群英传知识点回顾——第十章:Andro
  5. Android(安卓)View的加载过程
  6. ViewSwitcher使用范例
  7. Android(安卓)平台上SDCard的自动mount的
  8. android基本的五大布局(3)
  9. Android(安卓)深入解析AsyncTask(doInBack
  10. Android多媒体分析(六)对AudioManager的一