系统提供了很多可以直接调用的Activity,通过指定的Intent就可以调用,比如打开搜索的:
Java代码
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY,"searchString")
startActivity(intent);
Intent.ACTION_WEB_SEARCH是一个字符串,是“搜索”这个Activity的标识,extra是传给这个activity的一些数据。发送出这个intent之后,系统根据action字符串Intent.ACTION_WEB_SEARCH知道了是要调用哪个activity,如果有重名,会弹出一个选择对话框。然后打开此activity,实现想要做的事情。
那么,我们自己怎么来实现呢。
首先,写一个activity,在AndroidManifest.xml里面的intent-filter中,给这个activity命名,
Xml代码
< intent-filter>
< action android:name="chroya.foo"/>
< category android:name="android.intent.category.DEFAULT"/>
< /intent-filter>
< intent-filter>
< action android:name="chroya.foo"/>
< category android:name="android.intent.category.DEFAULT"/>
< /intent-filter>
然后安装。安装完毕之后,你会发现,系统中找不到这个程序。别急,它确实安装在手机里面了,但是因为他不是main的,所以系统不会把他当做Application的入口程序。
而要想打开这个activity,只有知道它名字的人才可以。跟系统的intent一样使用。它的名字定义为"chroya.foo",所以,这里用这个字符串就可以调用它了:
Java代码
Intent intent = new Intent("chroya.foo");
startActivity(intent);
Intent intent = new Intent("chroya.foo");
startActivity(intent);
我用刚才举的那个系统的intent说明,它的activity里面使用 getIntent().getBundleExtra(SearchManager.QUERY)来接收传递进来的搜索字符串参数。而这个 SearchManager.QUERY是关键字。如果要自己实现这种功能,只需要定义好关键字,然后从BundleExtra中取就行了
http://www.eoeandroid.com/forum-redirect-tid-66701-goto-lastpost.html#lastpost
看到论坛上有很多问亮度的问题(只能改变当前的Activity的问题,这个程序可以改变整个System的亮度)希望可以给朋友们带来帮助.
package com.jimmy; import android.app.Activity; import android.os.Bundle; import android.provider.Settings; import android.view.Window; import android.view.WindowManager; import android.widget.SeekBar; import android.widget.TextView; import android.widget.SeekBar.OnSeekBarChangeListener; public class MyActivity extends Activity { /** Called when the activity is first created. */ TextView textView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView = (TextView) findViewById(R.id.MyTextView); updateToggles(); } private void updateToggles() { // TODO Auto-generated method stub SeekBar seekBar = (SeekBar) findViewById(R.id.MySeekBar); seekBar.setProgress((int) (android.provider.Settings.System.getInt( getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, 255) )); seekBar.setOnSeekBarChangeListener(seekListener); } private OnSeekBarChangeListener seekListener = new OnSeekBarChangeListener() { public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (fromUser) { Integer tmpInt = seekBar.getProgress(); System.out.println(tmpInt); // 51 (seek scale) * 5 = 255 (max brightness) // Old way android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, tmpInt); // 0-255 tmpInt = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, -1); // Cupcake way..... sucks WindowManager.LayoutParams lp = getWindow().getAttributes(); // lp.screenBrightness = 1.0f; // Float tmpFloat = (float)tmpInt / 255; if (0<= tmpInt && tmpInt <= 255) { lp.screenBrightness = tmpInt; } getWindow().setAttributes(lp); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub // put awesomeness here } @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub // and here too } }; }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/white"> <TextView android:id="@+id/MyTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="亮度是:" /> <SeekBar android:layout_gravity="center_horizontal" android:id="@+id/MySeekBar" android:paddingLeft="5.0dip" android:paddingRight="5.0dip" android:layout_width="fill_parent" android:layout_height="150dip" android:layout_marginTop="10.0dip" android:layout_marginBottom="10.0dip" android:max="255" > </SeekBar> </LinearLayout>
权限:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jimmy" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MyActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="5" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.BATTERY_STATS"></uses-permission> <uses-permission android:name="android.permission.DEVICE_POWER"></uses-permission> <uses-permission android:name="android.permission.SET_DEBUG_APP"></uses-permission> <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission> <uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> </manifest>
通过长按menu按键,可以旋转屏幕方向(0°或者90°),这个功能有时蛮有用的,下面来看看是如何实现的: 1 修改按键处理程序
frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindowManager.java
函数
public boolean interceptKeyTi(WindowState win, int code, int metaKeys, boolean down,
int repeatCount, int flags)
在处理菜单键的地方
if (code == KeyEvent.KEYCODE_MENU) {
final int chordBug = KeyEvent.META_SHIFT_ON;
if (down && repeatCount == 0) {
if (mEnableShiftMenuBugReports && (metaKeys & chordBug) == chordBug) {
Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
mContext.sendOrderedBroadcast(intent, null);
return true;
} else if (SHOW_PROCESSES_ON_ALT_MENU &&
(metaKeys & KeyEvent.META_ALT_ON) == KeyEvent.META_ALT_ON) {
Intent service = new Intent();
service.setClassName(mContext, "com.android.server.LoadAverageService");
ContentResolver res = mContext.getContentResolver();
boolean shown = Settings.System.getInt(
res, Settings.System.SHOW_PROCESSES, 0) != 0;
if (!shown) {
mContext.startService(service);
} else {
mContext.stopService(service);
}
Settings.System.putInt(
res, Settings.System.SHOW_PROCESSES, shown ? 0 : 1);
return true;
}
}
//上面是原来的内容,下面是添加的新内容
else if (down && repeatCount == 20 && MenuKeyUp && (!keyguardOn)) {
//如果按下Menu键一定时间,抬起时执行此段函数
MenuKeyUp = false;
try {
int ro = mWindowManager.getRotation(); //获取当前方向
if( ro == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ) {
ro = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
} else {
ro = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
}
}
catch (RemoteException e) {
Log.v(TAG, "!!! getRotation fail !!!");
}
try {
//旋转屏幕
mWindowManager.setRotation(ro, true, Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
//最后可跟不同的参数,可实现一些旋转效果
}
catch (RemoteException e) {
Log.v(TAG, "!!! mWindowManager.setRotation fail !!!");
}
return true;
}
if(!down) {
MenuKeyUp = true;
}
}
2 修改实现选择的函数
/frameworks/base/services/java/com/android/server/WindowManagerService.java
找到该函数
public boolean setRotationUncheckedLocked(int rotation, int animFlags)
将以下妨碍选择的内容注释掉
//rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
// mRotation, mDisplayEnabled);
3、当然也可以新作一个rotate键来选择屏幕,以下是引用代码
+ } else if (code == KeyEvent.KEYCODE_ROTATE) {
+ // ROTATE KEY pressed
+ if (down) {
+ mButtonPushFlg = true;
+
+ try {
+ int ro = mWindowManager.getRotation(); // Orientation vertical
+ if (ro == 3 ) {
+ mWindowManager.setRotation (Surface.ROTATION_0,true,mFancyRotationAnimation); //Orientation
landscape
+ } else {
+ mWindowManager.setRotation
(Surface.ROTATION_270,true,mFancyRotationAnimation); //Orientation
portlate
+ }
+ } catch (RemoteException e) {
+ // Igbore
+ Log.i("info", "Rotation failed ");
+ }
+ }
+ return true;
}
OK,重新编译后,长按Menu键即可实现屏幕旋转。