<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
public void click(View v) {
//[1]獲取用戶名
String name = et_name.getText().toString().trim();
if (TextUtils.isEmpty(name)) {
Toast.makeText(getApplicationContext(), "用戶名不能爲空", Toast.LENGTH_LONG).show();
return;
}
//[2]判斷選中性別
int checkedRadioButtonId = rg_group.getCheckedRadioButtonId();
//[3]判斷一下具體選中的性別
int sex = 0; //默認值爲0
switch (checkedRadioButtonId) {
case R.id.rb_male: //選中的是男
sex = 1;
break;
case R.id.rb_female: //選中的是女
sex = 2;
break;
case R.id.rb_other: //代表選中的是人妖
sex = 3;
break;
}
//[4]判斷性別
if (sex == 0) {
Toast.makeText(getApplicationContext(), "親 請選擇性別 ", 0).show();
return;
}
//[5] 跳轉到resutActivity頁面 顯示意圖
Intent intent = new Intent(this,ResultActivity.class);
//[5.1]要把name 和 sex 傳遞到結果頁面 底層map
intent.putExtra("name", name);
intent.putExtra("sex", sex);
//[6]開啓Activity
startActivity(intent);
}
super.onCreate(savedInstanceState);
// 加載頁面
setContentView(R.layout.activity_result);
// [1]找到我們關心的控件
TextView tv_name = (TextView) findViewById(R.id.tv_name);
TextView tv_sex = (TextView) findViewById(R.id.tv_sex);
TextView tv_result = (TextView) findViewById(R.id.tv_result);
// [2]獲取開啓此Activity的意圖對象
Intent intent = getIntent();
// [3]獲取我們攜帶過來的數據 取出性別和name 傳遞的是什麼樣的數據類型 你在取的時候
String name = intent.getStringExtra("name");// 獲取name
int sex = intent.getIntExtra("sex", 0);
// [4]把數據顯示到控件上
tv_name.setText(name); // 顯示姓名
// [5]顯示性別
byte[] bytes = null;
try {
switch (sex) {
case 1: // 代表男
tv_sex.setText("男");
bytes = name.getBytes("gbk");
break;
case 2:
tv_sex.setText("女");
bytes = name.getBytes("utf-8");
break;
case 3:
tv_sex.setText("人妖");
bytes= name.getBytes("iso-8859-1");
break;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//[6]根據我們輸入的姓名和性別 來計算人品得分 根據得分顯示結果
int total = 0;
for (byte b : bytes) { //0001 1000
int number =b&0xff; //1111 1111
total+=number;
}
//算出得分
int score = Math.abs(total)%100;
if (score >90) {
tv_result.setText("您的人品非常好 您家的祖墳都冒青煙了");
}else if (score >70) {
tv_result.setText("有你這樣的人品算是不錯了..");
}else if (score >60) {
tv_result.setText("您的人品剛剛及格");
}else{
tv_result.setText("您的人品不及格....");
}
//(1)獲取控件
ListView lv = (ListView) findViewById(R.id.lv);
//(2)lv展示數據需要數據適配器 adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.item, objects);
//(3)設置數據適配器
lv.setAdapter(adapter);
//(4)設置條目的監聽
lv.setOnItemClickListener(new OnItemClickListener() {
//當條目被點擊的時候調用
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//(5) 獲取我們點擊條目的數據 小技巧 數據在哪裏存着呢 就去哪裏去
String content = objects[position];
System.out.println("content:"+content);
//(6)跳轉到發送短信頁面 用隱式意圖
/*<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>*/
//(7)創建意圖對象
Intent intent = new Intent();
intent.setAction("android.intent.action.SEND");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("text/plain");
//(7.1)把數據傳遞到 短信的發送頁面
intent.putExtra("sms_body", content); //要求大家知道這個是怎麼來的 ??
//(8)開啓意圖
startActivity(intent);
}
});
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入要發送的號碼 " />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:layout_alignParentRight="true"
android:layout_alignBottom="@id/et_number"
android:text="+" />
</RelativeLayout>
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="250dp"
android:gravity="top"
android:hint="請輸入要發送的內容 " />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="insert"
android:text="插入短信模板" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="send"
android:text="發送" />
</LinearLayout>
package com.itheima.smssender;
public class MainActivity extends Activity {
private EditText et_number;
private EditText et_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//顯示短信發送的內容
et_content = (EditText) findViewById(R.id.et_content);
//顯示號碼
et_number = (EditText) findViewById(R.id.et_number);
}
//點擊按鈕 跳轉到聯繫人頁面
public void click(View v){
//創建一個意圖對象 跳轉聯繫人頁面
Intent intent = new Intent(this,ContactActivity.class);
//如果你調用這個方法 就是簡單開啓頁面 不能拿到返回的數據 注意:如果你想開啓一個activity 並且想要獲取開啓的這個activity返回的數據 要調用這個方法
startActivityForResult(intent, 1);
// startActivity(intent);
}
//點擊按鈕 實現發送短信短信的邏輯
public void send(View v){
//TODO 一會實現
}
//點擊按鈕 打開插入短信模板 頁面
public void insert(View v){
Intent intent = new Intent(this,SmsTemplateActivity.class);
startActivityForResult(intent, 2);
}
//當這個activity(MainActivity) 開啓聯繫人頁面時 當聯繫人頁面關閉的時候這個方法會調用
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("onActivityResult");
/*if (resultCode == 10) {
//代表這個數據 是從聯繫人頁面獲取的
//(1)取出我們在聯繫人頁面設置的數據
String phone = data.getStringExtra("phone");
et_number.setText(phone);
}else if (resultCode == 20) {
//(2)把短信模板頁面的數據顯示到et_content控件上
String smscontent = data.getStringExtra("smscontent");
et_content.setText(smscontent);
}
*/
if (requestCode == 1) {
//代表請求聯繫人頁面
//(1)取出我們在聯繫人頁面設置的數據
String phone = data.getStringExtra("phone");
et_number.setText(phone);
}else if(requestCode == 2){
//請求短信模板頁面
//(2)把短信模板頁面的數據顯示到et_content控件上
String smscontent = data.getStringExtra("smscontent");
et_content.setText(smscontent);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
package com.itheima.smssender;
public class SmsTemplateActivity extends Activity {
String objects[] = {"我在開會,請稍後在打...","我在喫飯,請稍後在打...","我在打醬油,請稍後在打...","我在約會,請稍後在打...","我在打麻將,請稍後在打..."};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加載這個佈局
setContentView(R.layout.activity_smstemplate);
//(1)找到listview
ListView lv = (ListView) findViewById(R.id.lv);
//(2)把我們定義的好數據顯示到listview上
lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.smstemlpate_item, objects));
//(3)給listview設置點擊事件
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//(4)取出點擊條目的數據
String smscontent = objects[position];
//(5)當頁面關閉的時候把數據返回
Intent intent = new Intent();
intent.putExtra("smscontent", smscontent);
//(6)把數據返回給調用者
setResult(20, intent);
//(7)關閉這個Activity
finish();
}
});
}
}
//(2)實現發送短信的功能 smsManager 獲取這個類的實例
SmsManager smsManager = SmsManager.getDefault();
//(3)分割短信 分條發送
ArrayList<String> divideMessages = smsManager.divideMessage(content);
for (String div : divideMessages) {
//(4)發送短信
smsManager.sendTextMessage(number, null, div, null, null);
}
android.permission.SEND_SMS
- singletop 單一頂部模式 在activity的配置文件中設置android:launchMode="singleTop"
如果任務棧的棧頂存在這個要開啓的activity,不會重新的創建activity,而是複用已經存在的activity。保證棧頂如果存在,不會重複創建。
應用場景:瀏覽器的書籤
##13_singletask和singleinstance啓動模式
singetask 單一任務棧,在當前任務棧裏面只能有一個實例存在
當開啓activity的時候,就去檢查在任務棧裏面是否有實例已經存在,如果有實例存在就複用這個已經存在的activity,並且把這個activity上面的所有的別的activity都清空,複用這個已經存在的activity。保證整個任務棧裏面只有一個實例存在
應用場景:瀏覽器的activity
如果一個activity的創建需要佔用大量的系統資源(cpu,內存)一般配置這個activity爲singletask的啓動模式。
singleInstance啓動模式非常特殊, activity會運行在自己的任務棧裏面,並且這個任務棧裏面只有一個實例存在
如果你要保證一個activity在整個手機操作系統裏面只有一個實例存在,使用singleInstance
應用場景: 來電頁面