<span class="hljs-tag">&lt;<span class="hljs-name">extra</span> <span class="hljs-attr">android:name</span>=<span class="hljs-string">"setting_component_key"</span> <span class="hljs-attr">android:value</span>=<span class="hljs-string">"date_time"</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">intent</span> <span class="hljs-attr">android:targetPackage</span>=<span class="hljs-string">"com.android.settings"</span>
<span class="hljs-attr">android:targetClass</span>=<span class="hljs-string">"com.android.settings.Settings$DateTimeSettingsActivity"</span>/&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span></code><div class="hljs-button" data-title="复制"></div></pre><span style="font-family:Arial, Helvetica, sans-serif;background-color:rgb(255,255,255);"></span>
<p></p>
<pre></pre>
<p></p>
<p><span></span><span style="font-size:14px;">这是典型的一个设置中的选项,</span><span style="background-color:rgb(240,240,240);"><span style="font-size:14px;">DateTimeSettings.java 继承了Fragment。</span></span></p>
<p><span style="font-size:14px;">&nbsp; &nbsp; &nbsp;<span>所以</span>要添加一个选项,模仿即可。</span></p>
<p><span style="font-size:14px;">&nbsp; &nbsp; &nbsp;本例中以添加一个“老人模式”选项为例,为连线到一个APP中,即点击此选项将跳转到相对应的APP。只需这个应用的包名和类名,我们就可以把它嵌入到设置Settings中。</span></p>
<p><span style="font-size:14px;">&nbsp; &nbsp; &nbsp;1、首先在settings_hesder.xml中添加选项</span></p>
<p><span style="font-size:14px;"><span></span></span></p>
<pre onclick="hljs.copyCode(event)"><code class="language-html hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">header</span>
<span class="hljs-attr">android:id</span>=<span class="hljs-string">"@+id/aged_mode_setting_new"</span>
<span class="hljs-attr">android:icon</span>=<span class="hljs-string">"@drawable/ic_settings_aged_mode"</span>
<span class="hljs-attr">android:title</span>=<span class="hljs-string">"@string/aged_mode_setting_title"</span> &gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">intent</span> <span class="hljs-attr">android:action</span>=<span class="hljs-string">"com.hll.haolauncher"</span>/&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span></code><div class="hljs-button" data-title="复制"></div></pre>
<p></p>
<p><span style="font-size:14px;">&nbsp; &nbsp; &nbsp;其中,com.hll.haolauncher是目标应用的包名。<br></span></p>
<p><span style="font-size:14px;"><br></span></p>
<p><span style="font-size:14px;">&nbsp; &nbsp; &nbsp;2、在SettingsActivity.java中添加该选项显示</span></p>
<p><span style="font-size:14px;"></span></p>
<pre onclick="hljs.copyCode(event)"><code class="language-java hljs">&lt;pre name=<span class="hljs-string">"code"</span> <span class="hljs-class"><span class="hljs-keyword">class</span></span>=<span class="hljs-string">"java"</span>&gt;<span class="hljs-comment">// Show only these settings for restricted users</span>
<span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span>[] SETTINGS_FOR_RESTRICTED = {
R.id.wireless_section,
R.id.wifi_settings,
......
R.id.aged_mode_setting_new
};</code><div class="hljs-button" data-title="复制"></div></pre><br><pre></pre>
&nbsp; &nbsp; &nbsp;3、找到updateHeaderList方法,更新Settings选项的状态,代码大概如下:
<p></p>
<p><span style="font-size:14px;"></span></p>
<pre onclick="hljs.copyCode(event)"><code class="language-java hljs"><span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">updateHeaderList</span><span class="hljs-params">(List&lt;Header&gt; target)</span> </span>{
PDebug.Start(<span class="hljs-string">"updateHeaderList"</span>);
<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>;
mHeaderIndexMap.clear();
<span class="hljs-keyword">while</span> (i &lt; target.size()) {
Header header = target.get(i);
<span class="hljs-keyword">int</span> id = (<span class="hljs-keyword">int</span>) header.id;
<span class="hljs-keyword">if</span> (id == R.id.bluetooth_settings) {
<span class="hljs-keyword">if</span> (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
target.remove(i);
}
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (id == R.id.battery_settings) {
<span class="hljs-keyword">if</span> (!mBatteryPresent) {
target.remove(i);
}
}
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(id==R.id.aged_mode_setting) {
<span class="hljs-keyword">if</span> (enable){
target.remove(header);
}
}
PDebug.End(<span class="hljs-string">"updateHeaderList"</span>);
}</code><div class="hljs-button" data-title="复制"></div></pre>&nbsp;一些选项的显示或隐藏将被写到这里面。
<p></p>
<p><span style="font-size:14px;"><br></span></p>
<p><span style="font-size:14px;">&nbsp; &nbsp; &nbsp; &nbsp;4、设置Settings中的点击事件被写在onHeaderClick方法中,<span style="font-size:14px;">如下:</span></span></p>
<p><span style="font-size:14px;"></span></p>
<pre onclick="hljs.copyCode(event)"><code class="language-java hljs"><span class="hljs-keyword">if</span> (header.id ==R.id.aged_mode_setting_new)
{
Intent mIntent = <span class="hljs-keyword">new</span> Intent();
mIntent.setClassName(<span class="hljs-string">"com.hll.haolauncher"</span>,<span class="hljs-string">"com.hll.elauncher.StartActivity"</span>);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mIntent);
<span class="hljs-keyword">return</span>;
}</code><div class="hljs-button" data-title="复制"></div></pre><br><span></span>过程基本上是如此,由于笔者手上的代码不是原生系统的源码,不能把多一些的源码贴上,其中可能与原生代码有些出入,但大同小异,大都数地方还是相同的。
<p></p>
<p><span style="font-size:14px;"><br><br></span></p>
<p><span style="font-size:14px;"><br></span></p>
</div>
<script type="text/javascript">
$(function () {

Prev | Next
Pg.: 1 ... 10 11 12 13 14 15 16 17 18 19 20


Back to home | File page

Subscribe | Register | Login | N