Android定制--------开机对话框的实现
来源:互联网 发布:最难就业的专业 知乎 编辑:程序博客网 时间:2024/05/16 17:38
客户新要求:在每次开机的时候谈出对话框,对话框里面有连接他们公司网页的超链接,对话框还可以选择性的弹出。类似下图:
针对上面的需求整理下有几个需要注意的点:
1.每次开机弹出对话框
2.对话框中有超连接
3.对话框选择性的弹出
第2和3点都非常简单,等下看代码就秒懂了就不详细说了。就是第1点貌似有点麻烦。我的做法是这样,创建一个类似Launcher的运用(取名为BeforeLauncher,方便下面写)把它的优先级设置的比Launcher高,把该应用的Activity设置成Dialog样式。BeforeLauncher结束的时候用PackageManager的setComponentEnabledSetting方法将自己disable掉,不然在BeforeLauncher结束的时候Launcher就没法正常启动了。在调用PackageManager的setComponentEnabledSetting后会将disable掉的Component写进文件存到/data分区,下次开机后还会处于disable状态,这样就不能满足每次弹出对话框的需求。所以在每次开机第一次启动Launcher之前,重新enable这个Component。
看具体的文件和相关代码:
BeforeLauncher的AndroidManifest.xml具体内容:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.beforelauncher" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.beforelauncher.MainActivity" android:label="@string/app_name" android:launchMode="singleTask" android:clearTaskOnLaunch="true" android:stateNotNeeded="true" android:theme="@style/DialogTheme" android:screenOrientation="nosensor" android:configChanges="keyboard|keyboardHidden|navigation" android:taskAffinity="" android:enabled="true" > <intent-filter android:priority="800"> <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>这里要注意就是三个地方:
1.Activity的主题,android:theme="@style/AppTheme",看下主题里面具体有那些属性:
<style name="DialogTheme" parent="android:Theme.Holo.Light.Dialog"> <item name="android:windowCloseOnTouchOutside">false</item> <item name="android:windowShowWallpaper">true</item> <item name="android:windowNoTitle">true</item> </style>
2.优先级,android:priority="800",Android原生Launcher是默认的优先级,也就是0。
3. 将该应用设置成Launcher,<category android:name="android.intent.category.HOME" />,这个就不用说了,系统这解析AndroidManifest.xml文件的时候有有这个属性就默认该应用是Launcher。
BeforeLauncher的MainActivity具体内容:
<pre name="code" class="html">package com.example.beforelauncher;import android.os.Bundle;import android.app.Activity;import android.content.ComponentName;import android.content.pm.PackageManager;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.CheckBox;import android.widget.CompoundButton;import android.text.method.LinkMovementMethod;import android.content.SharedPreferences;import android.content.Context;public class MainActivity extends Activity { private Button mOkButton ;private TextView textView;private CheckBox checkBox;private SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.toastview);sp = getSharedPreferences("show_dialog", Context.MODE_PRIVATE);if(sp.getBoolean("show_dialog", false) )finish(); mOkButton = (Button)findViewById(R.id.acepter_button); mOkButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { finish(); } });textView=(TextView)findViewById(R.id.text2);textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { } }); textView.setMovementMethod(LinkMovementMethod.getInstance());checkBox = (CheckBox) findViewById(R.id.checkbox); checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { SharedPreferences.Editor editor=sp.edit(); editor.putBoolean("show_dialog",b); editor.apply(); } });} @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; } @Override public void finish() { PackageManager pm = getPackageManager(); ComponentName name = new ComponentName(MainActivity.this, MainActivity.class); try{ pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); }catch(Exception e){ } super.finish(); } @Override public void onBackPressed() { //super.onBackPressed(); }}
这里需要注意的就是,在Activity的finish方法中如下代码:
PackageManager pm = getPackageManager(); ComponentName name = new ComponentName(MainActivity.this, MainActivity.class); try{ pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); }catch(Exception e){ }上面这锻代码就是刚刚提到的在该Activity结束前要将该应相关的组件disable掉,不然Launher就无法正常启动。
那么有需要在那个合适的位置在将该应用相关的组件enable呢?就是在第一次启动Launcher的时候,就是在ActivityManagerService中的systemReady方法将其enable,不然下次开机这个Dialog又会无法正常弹出了,具体见如下代码:
public void systemReady(final Runnable goingCallback) { //此处省略很多代码 mBooting = true; //start PackageManager pm = mContext.getPackageManager(); String packageName = "com.example.beforelauncher"; ComponentName componentName = new ComponentName("com.example.beforelauncher", "com.example.beforelauncher.MainActivity"); try{ pm.setApplicationEnabledSetting(packageName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); }catch(Exception e){ } //end //此处省略很多代码 }
嗯、就这样了。
- Android定制--------开机对话框的实现
- 定制android系统的开机动画
- 定制android的开机启动界面
- Android系统的定制---定制系统开机动画
- Android 开机动画定制
- android 开机画面定制
- Android开机应用程序定制
- android 定制对话框 Layoutlnflater
- Android 入门 - 定制开机画面
- android系统开机logo定制
- Android系统定制开机动画
- Android如何定制开机Logo
- 【android】开机动画animation定制
- Android对话框的实现
- Android开机动画、logo、字样的定制过程
- android开机向导的实现
- android开机向导的实现
- 定制eclipse的about对话框
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- 人脸识别 - A Discriminative Feature Learning Approach for Deep Face Recognition
- |洛谷|堆|P1631 序列合并
- 函数指针与指针函数
- 日志监控及Redis存储
- Android定制--------开机对话框的实现
- Git图形界面的使用
- RAD Studio D10发布IOS TestFlight不支持外部测试的解决方法
- |洛谷|堆|P3045 [USACO12FEB]牛券Cow Coupons
- 网络专业化销售流程
- STM32之启动模式
- 安卓高级 特效动画ExplosionField和 SmoothTransition
- 对权限的理解
- 经典排序算法