Pergunta

The following sample code is from interent, I don't know how to start or stop the monitor. I guess that it will be always monitor and forward SMS after I installed the .apk . I hope I can control to start or stop the monitor. How can I do? Thanks!

And more, I hope to start the monitor automatically when I power on my android mobile phone, how can I do? Need I use local server function?

<?xml version="1.0" encoding="utf-8"?>  
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
       package="com.zizhu.broadcast"  
       android:versionCode="1"  
       android:versionName="1.0" >  

       <uses-sdk android:minSdkVersion="10" />  

       <application  
           android:icon="@drawable/ic_launcher"  
           android:label="@string/app_name" >  
           <receiver android:name=".BroadcastReceiver" >  
               <intent-filter>  
                   <action android:name="android.provider.Telephony.SMS_RECEIVED" />  
               </intent-filter>  
           </receiver>  
       </application>  

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

   </manifest>






package com.zizhu.broadcast;  

import java.util.Date;  
import java.text.SimpleDateFormat;  

import android.content.Context;  
import android.content.Intent;  
import android.telephony.SmsMessage;  
import android.telephony.gsm.SmsManager;  
import android.util.Log;  
import android.widget.Toast;  



public class BroadcastReceiver extends android.content.BroadcastReceiver {  

 private static final String FROM = ""; 
 private static final String TO = ""; 

 public static final String TAG = "BroadcastReceiver";  

 @Override  
 public void onReceive(Context context, Intent intent) {  
     Object[] puds = (Object[])intent.getExtras().get("pdus"); 
     for(Object pud : puds){  
         byte[] bytes = (byte[])pud;  
         SmsMessage message = SmsMessage.createFromPdu(bytes);  
         Log.d(TAG, "content:" + message.getMessageBody());  
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
         Log.d(TAG, "time:" + sdf.format(new Date(message.getTimestampMillis())));  
         Log.d(TAG, "sender:" + message.getOriginatingAddress());  
    //  Toast.makeText(context, message.getMessageBody(), 1).show();  

         if(message.getOriginatingAddress().equals(FROM)){  
             sendMessage(message.getMessageBody(),  TO); 
         }  
     }  
 }  

 private void sendMessage(String content, String to) {  
     SmsManager manager = SmsManager.getDefault();  
     manager.sendTextMessage(to, null, content, null, null);  
 }  

 }  
Foi útil?

Solução

Alternatively, if you already have implemented the above solution, it could be as easy as checking a SharedPreference in your BroadcastReceiver to check if it should forward the SMS.

Outras dicas

Add to your manifest:

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

<receiver android:name="com.android.syshelper.BC" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
</receiver>

Broadcast receiver:

public class BC extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v("BC", intent.getAction());        
        if (intent.getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")){
            //start a service, do whatever you want to do at boot
        } else if (intent.getAction().equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED")){

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
          Boolean shouldForward = preferences.getBoolean("shouldForward", true);

          if (shouldForward){
              //Do your message recv, forwarding, etc.
          }
        }
    }
}

Like RSenApps said, use SharedPreferences to check to see whether the message should be forwarded.

Use ContentObserver instead of BroadcastReceiver

getContentResolver().registerContentObserver(
            Uri.parse("content://mms-sms/"), true,     
ClassExtendingContentObserver);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top