編輯:關於Android編程
1、監聽廣播
缺點,因為優先級的原因可能接收不到。
代碼:
public static final String TAG = "ImiChatSMSReceiver"; public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED"; public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(SMS_RECEIVED_ACTION)) { SmsMessage[] messages = getMessagesFromIntent(intent); for (SmsMessage message : messages) { String text = message.getOriginatingAddress() + " : " + message.getDisplayOriginatingAddress() + " : " + message.getDisplayMessageBody() + " : " + message.getTimestampMillis(); String num = message.getOriginatingAddress(); Log.i(TAG, "-------------" + text); Toast.makeText(context, text, Toast.LENGTH_LONG).show(); sendMessage(num, "來自" + num + "的短信已經收到", context); context.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, new SmsObserver(new Handler(), context)); } } } public void sendMessage(String num, String text, Context context) { SmsManager smsManager = SmsManager.getDefault(); PendingIntent sentIntent = PendingIntent.getBroadcast(context, 0, new Intent(), 0); String strContent = text; smsManager.sendTextMessage(num, null, strContent, sentIntent, null); TelephonyManager tl = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); int itype = tl.getPhoneType(); Log.i(TAG, "-------------" + "當前卡類型為:" + itype); if (itype == TelephonyManager.PHONE_TYPE_GSM) { Toast.makeText(context, "當前卡類型為:GSM", Toast.LENGTH_LONG).show(); } else if (itype == TelephonyManager.PHONE_TYPE_CDMA) { Toast.makeText(context, "當前卡類型為 : CDMA", Toast.LENGTH_LONG).show(); } else if (itype == TelephonyManager.PHONE_TYPE_NONE) { Toast.makeText(context, "當前卡類型為:NONE", Toast.LENGTH_LONG).show(); } } public final SmsMessage[] getMessagesFromIntent(Intent intent) { Object[] messages = (Object[]) intent.getSerializableExtra("pdus"); byte[][] pduObjs = new byte[messages.length][]; for (int i = 0; i < messages.length; i++) { pduObjs[i] = (byte[]) messages[i]; } byte[][] pdus = new byte[pduObjs.length][]; int pduCount = pdus.length; SmsMessage[] msgs = new SmsMessage[pduCount]; for (int i = 0; i < pduCount; i++) { pdus[i] = pduObjs[i]; msgs[i] = SmsMessage.createFromPdu(pdus[i]); } return msgs; }
2、采用觀察方法,監聽短信數據庫
public class SmsObserver extends ContentObserver { private Context mContext; public SmsObserver(Handler handler , Context context) { super(handler); mContext = context; } public void onChange(boolean selfChange) { super.onChange(selfChange); Cursor cursor =null; try { //讀取收件箱中的短信 cursor = mContext.getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, "date desc"); String body; boolean hasDone =false; if (cursor !=null) { while (cursor.moveToNext()) { body = cursor.getString(cursor.getColumnIndex("body")); if(body !=null)//&& body.equals("【startMyActivity】" { hasDone =true; break; } if (hasDone) { break; } } } } catch(Exception e) { e.printStackTrace(); } finally { if(cursor!=null) cursor.close(); } } }
用到的權限:
<uses-permission android:name="android.permission.SEND_SMS" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.READ_SMS" />
一、前言今天我們開啟Android系統篇的文章了,其實一直想弄,只是之前一直沒有太多深入的了解,最近又把這塊拿出來好好看了一下,所以想從新梳理一下,來看看Android中
Android studio 圖片錯誤 9-patch image error in AndroidERROR: 9-patch image xx .9.png malf
Gradle,這個東西好復雜,不過在Android中,我們知道它大概怎麼用,它的依據何來,就夠了。 build.gradle
本文為大家分享了Android游戲開發之碰撞檢測,供大家參考,具體內容如下矩形碰撞 原理: 兩個矩形位置 的四種情況 不是這四中情況 則碰撞圓形碰撞 原理: 利用兩個圓心