[转自网上]
第一种方式,用action来跳转。
1、使用Action跳转,如果有一个程序的AndroidManifest.xml中的某一个 Activity的IntentFilter段中 定义了包含了相同的Action那么这个Intent就与这个目标Action匹配。如果这个IntentFilter段中没有定义 Type,Category,那么这个 Activity就匹配了。但是如果手机中有两个以上的程序匹配,那么就会弹出一个对话可框来提示说明。
Action 的值在Android中有很多预定义,如果你想直接转到你自己定义的Intent接收者,你可以在接收者的IntentFilter 中加入一个自定义的Action值(同时要设定 Category值为"android.intent.category.DEFAULT"),在你的Intent中设定该值为Intent的 Action,就直接能跳转到你自己的Intent接收者中。因为这个Action在系统中是唯一的。
2,data/type,你可以用Uri 来做为data,比如Uri uri = Uri.parse(http://www.google.com);
Intent i = new Intent(Intent.ACTION_VIEW,uri);手机的Intent分发过程中,会根据http://www.google.com 的scheme判断出数据类型type
手机的Brower则能匹配它,在Brower的Manifest.xml中的IntenFilter中 首先有ACTION_VIEW Action,也能处理http:的type,
3, 至于分类Category,一般不要去在Intent中设置它,如果你写Intent的接收者,就在Manifest.xml的Activity的 IntentFilter中包含android.category.DEFAULT,这样所有不设置 Category(Intent.addCategory(String c);)的Intent都会与这个Category匹配。
4,extras(附 加信息),是其它所有附加信息的集合。使用extras可以为组件提供扩展信息,比如,如果要执行“发送电子邮件”这个动 作,可以将电子邮件的标题、正文等保存在extras里,传给电子邮件发送组件。
Java 代码
package com.android.edit_text;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
public class MyEditText extends Activity {
private TextView m_TextView;
private EditText m_EditText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_EditText = (EditText) this.findViewById(R.id.EditText01);
m_EditText.setOnKeyListener(editTextKeyListener);
}
private EditText.OnKeyListener editTextKeyListener = new EditText.OnKeyListener() {
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
// action 跳转,需要在AndroidManifest.xml中配置action
Intent i = new Intent("android.intent.action.mydialog");
MyEditText.this.startActivity(i);
return false;
}
};
}
Xml代 码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.edit_text" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyEditText" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 配置跳转activity-->
<activity android:name="com.android.dialog.MyDialog">
<intent-filter>
<!-- 配置action路径-->
<action android:name="android.intent.action.mydialog" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
第二种方式,用类名跳转。
Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描 述,Android则根据此Intent的描述, 负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。Intent在这里起着实现调用者与被调用者之间的解耦作用。
Intent传递过程中,要找 到目标消费者(另一个Activity,IntentReceiver或Service),也就是Intent的响 应者。
Java 代码
package com.Android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class FormStuff extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.formstuff);
final ImageButton button = (ImageButton) findViewById(R.id.android_button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 用 类名跳转,需要在AndroidManifest.xml中申明activity
Intent intent = new Intent(FormStuff.this, HelloTabWidget.class);
startActivity(intent);
}
});
}
Xml代 码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Android" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:theme="@android:style/Theme.NoTitleBar">
<activity android:name=".FormStuff" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 申明activity-->
<activity android:name="HelloTabWidget"></activity>
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>
下面列出几种Intent的用法
显示网页:
Uri uri = Uri.parse("http://www.google.com");
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
显示地图:
Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.Action_VIEW,uri);
startActivity(it);
路径规划:
Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat startLng&daddr=endLat endLng&hl=en");
Intent it = new Intent(Intent.ACTION_VIEW,URI);
startActivity(it);
拨打电话:
调用拨号程序
Uri uri = Uri.parse("tel:xxxxxx");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
Uri uri = Uri.parse("tel.xxxxxx");
Intent it =new Intent(Intent.ACTION_CALL,uri);
要使用这个必须在配置文件中加入<uses-permission id="android.permission.CALL_PHONE" />
发送SMS/MMS
调用发送短信的程序
Intent it = new Intent(Intent.ACTION_VIEW);
it.putExtra("sms_body", "The SMS text");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);
发送短信
Uri uri = Uri.parse("smsto:0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
startActivity(it);
发送彩信
Uri uri = Uri.parse("content://media/external/images/media/23");
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra("sms_body", "some text");
it.putExtra(Intent.EXTRA_STREAM, uri);
it.setType("image/png");
startActivity(it);
发送Email
Uri uri = Uri.parse("mailto:xxx@abc.com");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.setType("text/plain");
startActivity(Intent.createChooser(it, "Choose Email Client"));
Intent it=new Intent(Intent.ACTION_SEND);
String[] tos={"me@abc.com"};
String[] ccs={"you@abc.com"};
it.putExtra(Intent.EXTRA_EMAIL, tos);
it.putExtra(Intent.EXTRA_CC, ccs);
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.setType("message/rfc822");
startActivity(Intent.createChooser(it, "Choose Email Client"));
添加附件
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");
sendIntent.setType("audio/mp3");
startActivity(Intent.createChooser(it, "Choose Email Client"));
播放多媒体
Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri, "audio/mp3");
startActivity(it);
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
Uninstall 程序
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);
Install 程序
Uri installUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
搜索应用
//搜索应用
Uri uri = Uri.parse("market://search?q=pname:pkg_name");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where pkg_name is the full package path for an application
//显示指定应用的详细页面(这个好像不支持了,找不到app_id)
Uri uri = Uri.parse("market://details?id=app_id");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where app_id is the application ID, find the ID
//by clicking on your application on Market home
//page, and notice the ID from the address bar
参考资料:
http://wallage.blog.163.com/blog/static/1738962420104264340803/
http://www.eoeandroid.com/viewthread.php?tid=266
http://hi.baidu.com/weiyousheng/blog/item/41a5533b587451e614cecb68.html
package com.test;
import java.io.FileWriter;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
Button stopBut;
Button clearBut;
TextView textView;
CheckBox mCheckBox1, mCheckBox2;
int bootNumber;
boolean isCBChecked;
boolean isReboot = false;
Context mContext;
private static final String BOOT_NUMBER = "mBootNumber";
private static final int SHUT_DOWN = 0;
private static final int PLAY_VIDEO = 1;
private static final String TAG = "MainActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this;
stopBut = (Button) findViewById(R.id.stopBut);
clearBut = (Button) findViewById(R.id.clearBut);
textView = (TextView) findViewById(R.id.mTextView);
mCheckBox1 = (CheckBox) findViewById(R.id.CheckBox1);
mCheckBox2 = (CheckBox) findViewById(R.id.CheckBox2);
stopBut.setOnClickListener(this);
clearBut.setOnClickListener(this);
onReboot();
// 启动保存log日志apk
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.logcat", "com.logcat.MainActivity"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
//Intent mIntent = new Intent();
//mIntent = getPackageManager().getLaunchIntentForPackage(packageName);
//startActivity(mIntent);
SharedPreferences pres = getSharedPreferences(BOOT_NUMBER,Context.MODE_WORLD_READABLE);
if (pres != null) {
bootNumber = pres.getInt("boot_Number", 0);
isCBChecked = pres.getBoolean("CheckBox_", true);
}
Log.i(TAG, "---------onCreate number:" + bootNumber);
textView.setText("开机次数:" + bootNumber);
bootNumber++;
if (isCBChecked) {
mCheckBox1.setChecked(true);
mCheckBox2.setChecked(false);
mHandler.sendEmptyMessageDelayed(SHUT_DOWN, 8000);
Log.i(TAG, "---------isCBChecked:mCheckBox1" );
} else {
mCheckBox1.setChecked(false);
mCheckBox2.setChecked(true);
mHandler.sendEmptyMessageDelayed(PLAY_VIDEO, 4000);
Log.i(TAG, "---------isCBChecked:mCheckBox2" );
}
mCheckBox1
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
SharedPreferences pres = getSharedPreferences(
BOOT_NUMBER, Context.MODE_WORLD_READABLE);
if (isChecked) {
isCBChecked = true;
mCheckBox1.setChecked(true);
mCheckBox2.setChecked(false);
if (pres != null) {
SharedPreferences.Editor ed = pres.edit();
ed.putBoolean("CheckBox_", true);
ed.commit();
}
} else {
isCBChecked = false;
SharedPreferences.Editor ed = pres.edit();
ed.putBoolean("CheckBox_", false);
ed.commit();
}
}
});
mCheckBox2
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
SharedPreferences pres = getSharedPreferences(
BOOT_NUMBER, Context.MODE_WORLD_READABLE);
if (isChecked) {
isCBChecked = false;
mCheckBox2.setChecked(true);
mCheckBox1.setChecked(false);
if (pres != null) {
SharedPreferences.Editor ed = pres.edit();
ed.putBoolean("CheckBox_", false);
ed.commit();
}
} else {
isCBChecked = true;
SharedPreferences.Editor ed = pres.edit();
ed.putBoolean("CheckBox_", true);
ed.commit();
}
}
});
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SHUT_DOWN:
onShutdown();
break;
case PLAY_VIDEO:
onPlayVideo();
break;
default:
break;
}
}
};
@Override
public void onClick(View view) {
if (view == stopBut) {
Log.i(TAG, " stop");
new AlertDialog.Builder(this).setMessage("停止重启设备").setPositiveButton("OK", null).show();
stop();
mHandler.removeMessages(SHUT_DOWN);
} else if (view == clearBut) {
Log.i(TAG, " clear");
SharedPreferences pres = getSharedPreferences(BOOT_NUMBER,Context.MODE_WORLD_READABLE);
if (pres != null) {
SharedPreferences.Editor ed = pres.edit();
ed.putInt("boot_Number", 0);
ed.commit();
}
bootNumber = 0;
textView.setText("开机次数:" + 0);
}
}
public void onShutdown() {
SharedPreferences pres = getSharedPreferences(BOOT_NUMBER,Context.MODE_WORLD_READABLE);
if (pres != null) {
SharedPreferences.Editor ed = pres.edit();
ed.putInt("boot_Number", bootNumber);
ed.commit();
}
//关机
PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
pm.reboot("recovery");
}
private void onPlayVideo(){
mHandler.sendEmptyMessageDelayed(SHUT_DOWN, 90000);
Intent playVideo = new Intent();
playVideo.setClass(MainActivity.this, VideoActicity.class);
startActivity(playVideo);
}
private void stop() {
try {
FileWriter fw = new FileWriter("/sys/misc/poweron_status");
fw.write("0x5a");
fw.close();
Log.i(TAG, "---------stop");
} catch (IOException e) {
e.printStackTrace();
new AlertDialog.Builder(this).setMessage(e.getMessage())
.setPositiveButton("OK", null).show();
}
}
//修改文件
private void onReboot() {
try {
FileWriter fw = new FileWriter("/sys/misc/poweron_status");
fw.write("0x87");
fw.close();
Log.i(TAG, "---------onReboot");
} catch (IOException e) {
e.printStackTrace();
new AlertDialog.Builder(this).setMessage(e.getMessage())
.setPositiveButton("OK", null).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeMessages(SHUT_DOWN);
mHandler.removeMessages(PLAY_VIDEO);
}
}
scanf()函数是格式化输入函数,它从标准输入设备(键盘) 读取输入的信息。
其调用格式为: scanf("<格式化字符串>",<地址表>);
格式化字符串包括以下三类不同的字符;
1、 格式化说明符:
格式化说明符与printf()函数中的格式说明符基本相同。但和printf()函数中格式字符串的用法有一些小区别。我们来看下面这个表。
格式字符 说明
%d 从键盘输入十进制整数
%o 从键盘输入八进制整数
%x 从键盘输入十六进制整数
%c 从键盘输入一个字符
%s 从键盘输入一个字符串
%f 从键盘输入一个实数
%e 与%f的作用相同
附加格式说明字符表
字符 说明
L 输入"长"数据
H 输入"短"数据
M 指定输入数据所占宽度
* 空读一个数据
2、 空白字符: 空白字符会使scanf()函数在读操作中略去输入中的一个或多个空白字符。
3、 非空白字符: 一个非空白字符会使scanf()函数在读入时剔除掉与这个非空白字符相同的字符。
地址表是需要读入的所有变量的地址,而不是变量本身。这与printf()函数完全不同,要特别注意。各个变量的地址之间同","分开。
首先我们来看一下格式化说明符“%d”,从键盘输入一个十进制整数,当多个输入时,采用一个或多个空格做为分隔符,看下面的例子:
运行结果如下:
分析如下:
scanf格式化输入中有多个空格和’\n’,但是我们采用的是%d格式化输入,所以空格和’\n’等转义字符是没有读入的,这里有一个”%*3d”,意思是跳过输入的前三个整型数字,从第四个数字开始读入,所以我们输入“432432432”,输出的却是432432,而不是432432432.
有同学就要问了,为什么输入“432432432”后还有输入一个字母’d’呢?其实这个地方输入任何一个非空字节都是可以的。因为scanf必须从缓冲区读入一个非空字符再回车才会刷新缓冲区,这里scanf已经把“432432432”读入变量,缓冲区里就为空了,所以必须输入一个非空字节,这样按回车才会结束scanf输入。
好,下面我们来看”%c”,还是先写一个例子:
运行结果如下:
分析:
这里一目了然,采用“%c”输入时,scanf不仅会读入非空字符,遇到空字符他也会读入。
再看一个例子:
#include <stdio.h>
void main()
{
char a,b;
printf("input charactera,b\n");
scanf("%c %c",&a,&b);/*注意两个%c之间有个空格*/
printf("\n%c%c\n",a,b);
}
本例表示scanf格式控制串"%c %c"之间有空格时, 输入的数据之间可以有空格间隔。这时候我们应该这样输入’a’空格’b’。
下面看一下”%s”,遇到空格换行等转义字符不读入,
分析:
这里一目了然,采用“%c”输入时,scanf不仅会读入非空字符,遇到空字符他也会读入。
再看一个例子:
#include <stdio.h>
void main()
{
char a,b;
printf("input charactera,b\n");
scanf("%c %c",&a,&b);/*注意两个%c之间有个空格*/
printf("\n%c%c\n",a,b);
}
本例表示scanf格式控制串"%c %c"之间有空格时, 输入的数据之间可以有空格间隔。这时候我们应该这样输入’a’空格’b’。
下面看一下”%s”,遇到空格换行等转义字符不读入,
运行结果如下: