android廣播實現簡訊竊聽器和開機自動啟動activity

來源:互聯網
上載者:User

一、簡訊竅聽器

首先:訂閱感興趣的廣播 Intent ,訂閱者法有兩種:
第一種:使用代碼進行訂閱

IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");IncomingSMSReceiver receiver = new IncomingSMSReceiver();registerReceiver(receiver, filter);

第二種:在 AndroidManifest.xml 檔案中的 <application> 節點裡進行訂閱 :

<receiver android:name=".IncomingSMSReceiver"><intent-filter><action android:name="android.provider.Telephony.SMS_RECEIVED"/></intent-filter></receiver>

咱用第二種:

在android下,要想接受廣播資訊,那麼這個廣播接收器就得我們自己來實現了,我們可以繼承BroadcastReceiver,就可以有一個廣播接受器了。有個接受器還不夠,我們還得重寫BroadcastReceiver裡面的onReceiver方法,當來廣播的時候我們要幹什麼,這就要我們自己來實現!!

public class MySMSListener extends BroadcastReceiver {public void onReceive(Context arg0, Intent intent) {Bundle bundle=intent.getExtras();Object[] pdus=(Object[])bundle.get("pdus");if(pdus!=null&&pdus.length>0){SmsMessage[] messages=new SmsMessage[pdus.length];for(int i=0;i<messages.length;i++){byte[] pdu=(byte[]) pdus[i];messages[i]=SmsMessage.createFromPdu(pdu);}for(SmsMessage msg:messages){String content=msg.getMessageBody();String sender=msg.getOriginatingAddress();Date date=new Date(msg.getTimestampMillis());SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String sendTime=sdf.format(date);if(sender!=null&& sender.endsWith("5556")){System.out.println("5556");SmsManager smsManager=SmsManager.getDefault();smsManager.sendTextMessage("5556", null, "go to !!", null, null);this.abortBroadcast();//終止廣播}}}}}

這裡需要啟動兩個模擬器!!

if語句判斷是不是5556來的簡訊,如果是,終止廣播,不讓5556發簡訊到5554,並給5556發一個簡訊,內容為“go to!!";

在這裡 , 不用理解到底什麼是 pdus ,只要記住是這麼用的就可以了!

然後就是在 AndroidManifest.xml 檔案中的 <application> 節點裡加入如下代碼,

註冊

<receiver android:name="MySMSListener"            >            <intent-filter android:priority="1000">                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>            </intent-filter>        </receiver>

這個priority是定義許可權,值是-1000~1000;
還要加上許可權申請:

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

ok!你可以貼代碼試試效果!!

二、開機自動啟動activity

繼承BroadcaseReceiver類

public class StartListenerActivity extends BroadcastReceiver{public void onReceive(Context context, Intent intent) {Log.i("TAG", "系統啟動完畢");String category = "android.intent.category.LAUNCHER";String action = "android.intent.action.MAIN";Intent myIntent = new Intent(context, DateAcitivty.class);//DateAcitivty為我的主程式介面myIntent.setAction(action);myIntent.addCategory(category);myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(myIntent);Log.i("TAG", "activity啟動完畢");}}

 AndroidManifest.xml 檔案

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="cn.csdn.listener"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="10" />    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <receiver             android:name=".StartListenerActivity">            <intent-filter>                <action android:name="android.intent.action.BOOT_COMPLETED"/>            </intent-filter>        </receiver>       <activity            android:name=".DateAcitivty">           <intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter>       </activity>    </application></manifest>

最後是給出activity類:

public class DateAcitivty extends Activity {TextView hello;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);hello=(TextView)findViewById(R.id.hello);Date date=new Date();SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String time=format.format(date);hello.setText(time);Toast.makeText(this, "成功"+time, Toast.LENGTH_LONG).show();}}

最後補充:

除了簡訊、開機啟動廣播 Intent , Android 還有很多廣播Intent ,如:電池電量變化、時間已經改變等廣播Intent 。

在Android 中如果要發送一個廣播必須使用sendBroadCast 向系統發送對其感興趣的廣播接收器中。

 使用廣播必須要有一個intent 對象必設定其action動作對象

使用廣播必須在設定檔中顯式的指明該廣播對象

 每次接收廣播都會重建一個接收廣播的對象

  在BroadCast 中盡量不要處理太多邏輯問題,建議複雜的邏輯交給Activity 或者 Service 去處理

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.