您的位置:首页 > 移动开发 > Android开发

android 添加菜单 开发流程 短信发送与拨打电话

2011-11-11 00:27 701 查看
1、添加菜单

public class MenuActivity extends Activity {

private static final String TAG = "MenuActivity";

private static final int MENU_ADD = Menu.FIRST;

private static final int MENU_UPDATE = Menu.FIRST + 1;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.menu);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

menu.add(Menu.NONE, MENU_ADD, Menu.NONE, "添加");

menu.add(Menu.NONE, MENU_UPDATE, Menu.NONE, "更新");

return super.onCreateOptionsMenu(menu);

}

@Override

public boolean onMenuItemSelected(int featureId, MenuItem item) {

switch (item.getItemId()) {

case MENU_ADD:

Log.i(TAG, "add was selected");

return true;

case MENU_UPDATE:

Log.i(TAG, "update was selected");

return true;

default:

break;

}

return super.onMenuItemSelected(featureId, item);

}

}

2、Android应用开发流程

①设计界面 ---View

②编写Activity ---Controller

③开发业务层 ---Model

3、电话拨号器(利用已有的电话拨打功能)

1>. 在strings.xml添加常用的文字内容,可添加多个类似strings.xml结构的文件,但是文件名称(不能相同)任意,文件结构要相同。

<resources>

<string name="app_name">网星电话拨号器</string>

<string name="input_code">请输入手机号码</string>

<string name="send">拨打此号码</string>

</resources>

2>. 设计界面

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/input_code"

/>

<EditText

android:id="@+id/mobile" -------定义控件的标识,采用@+id/mobile表达式,会在R文件的内部类中添加常量引用该EditText

android:layout_width="fill_parent"

android:layout_height="wrap_content"

-----android:text="可以给默认文字内容"

/>

<Button

android:id="@+id/send" ------------定义控件的标识,采用@+id/send表达式

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/send"

/>

3>. 预览界面

4>. 获取"发送"按钮的引用,类名称和标签名称相同

Button send = (Button)this.findViewById(R.id.send);

5>. 给"发送"按钮注册点击事件监听器

send.setOnClickListener(

new View.OnClickListener() {

public void onClick(View v) {

}

}

}

6>. 在按钮点击事件中发送Intent

//创建意图(Intent)对象

Intent intent = new Intent();

intent.setAction("android.intent.action.CALL");//也可以使用Intent.ACTION_CALL常量,参看\android-sdk-windows\platforms\android-8\sources\phone\AndroidManifest.xml 112行

intent.setData(Uri.parse("tel:"+ mobile));

7>. 添加default类别

startActivity(intent);

8>. 在项目清单文件中添加电话拨打的权限

<uses-permission android:name="android.permission.CALL_PHONE"/>

4、短信发送器

1>. 在strings.xml添加常用的文字内容,可添加多个类似strings.xml结构的文件,但是文件名称(不能相同)任意,文件结构要相同。

<resources>

<string name="app_name">网星短信发送器</string>

<string name="input_code">请输入手机号码</string>

<string name="input_sms_content">请输入短信内容</string>

<string name="send">发送短信</string>

<string name="success">发送完成</string>

</resources>

2>. 设计界面

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/input_code"

/>

<EditText

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/input_code"

/>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/input_sms_content"

/>

<EditText

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:minLines="3"

android:id="@+id/content"

/>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/send"

android:id="@+id/send"

/>

3>. 预览界面

4>. 获取"发送"按钮的引用,类名称和标签名称相同

Button send = (Button)this.findViewById(R.id.send);

5>. 给"发送"按钮注册点击事件监听器

send.setOnClickListener(

new View.OnClickListener() {

public void onClick(View v) {

}

}

}

6>. 发送短信

/** 获取默认的短信管理器 **/

SmsManager smsManager = SmsManager.getDefault();

/** 短信内容是有限制的,如果超过70个汉字需要拆分短信 **/

List<String> texts = smsManager.divideMessage(content);

for(String text : texts){

smsManager.sendTextMessage(mobile, null, text, null, null);

}

7>. 发送短信成功后提示

/** 提示方式: 状态栏通知、对话框通知、吐司(Toast)通知 **/

Toast.makeText(SMSActivity.this, R.string.success, 1).show();

8>. 在项目清单文件中添加发送短信的权限

<uses-permission android:name="android.permission.SEND_SMS"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: