打电话和发短信程序
首先添加按钮控件
?
<Button?
? ?android:id="@+id/but1"
? ?android:layout_width="wrap_content"
? ?android:layout_height="wrap_content"
? ?android:text="Call"
? ?/>
?
?
?
?package com.sun.hello;
import android.app.Activity;
import android.content.Intent;
import android.location.GpsStatus.Listener;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class HelloActivity extends Activity {
private Button button = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.but1);
button.setOnClickListener(listener);
}
private OnClickListener listener=new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent =new Intent(); ? //new一个意图
intent.setAction(Intent.ACTION_CALL);//动作
intent.setData(Uri.parse("tel:15226513509"));//设置电话号码
startActivity(intent);//启动
}
};
}
?
?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
? ? package="com.sun.hello"
? ? android:versionCode="1"
? ? android:versionName="1.0" >
? ? <uses-sdk android:minSdkVersion="15" />
? ? <application
? ? ? ? android:icon="@drawable/ic_launcher"
? ? ? ? android:label="@string/app_name" >
? ? ? ? <activity
? ? ? ? ? ? android:name=".HelloActivity"
? ? ? ? ? ? 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-permission android:name="android.permission.CALL_PHONE" />//增加打电话权限
</manifest>
//-============================================================
?package com.sun.hello;
?
import android.app.Activity;
import android.content.Intent;
import android.location.GpsStatus.Listener;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
?
public class HelloActivity extends Activity {
private Button button = null;
?
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.but1);
button.setOnClickListener(listener);
}
private OnClickListener listener=new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent =new Intent();
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:5554"));
intent.putExtra("sms_body", "Hello");
startActivity(intent);
}
};
}? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
? ? package="com.sun.hello"
? ? android:versionCode="1"
? ? android:versionName="1.0" >
?
? ? <uses-sdk android:minSdkVersion="15" />
?
? ? <application
? ? ? ? android:icon="@drawable/ic_launcher"
? ? ? ? android:label="@string/app_name" >
? ? ? ? <activity
? ? ? ? ? ? android:name=".HelloActivity"
? ? ? ? ? ? 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-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS"/>
</manifest>
?
?
?