Android - Try to send fake sms to myself without mobile network usage

前端 未结 1 1146
旧时难觅i
旧时难觅i 2020-12-24 14:51

I\'m trying to send message to my phone with this app, without using network usage, but my code doesn\'t work. I followed some tutorial, check android dev and I haven\'t fou

1条回答
  •  猫巷女王i
    2020-12-24 15:04

    With the help from Mike M. I finally finished my program. So, this is the code that you must add to your app to be able to send sms without using network:

    Manifest:

    
    
    
    
    
        
        
            
                
            
        
    
        
        
            
                
                
            
        
    
        
        
            
                
                
            
        
    
        
        
            
                
                
                
                
                
                
                
                
            
        
    
        
        
            
                
                
                
                
                
                
            
        
    
    
    

    Main Activity:

    Context context;
    Button button;
    String sender,body,defaultSmsApp;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        //Get current context
        context = this;
    
        //Set composant
        button = (Button) findViewById(R.id.button);
    
        //Get default sms app
        defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(context);
    
        //Set the number and the body for the sms
        sender = "0042";
        body = "Android fake message";
    
        //Button to write to the default sms app
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
    
                //Get the package name and check if my app is not the default sms app
                final String myPackageName = getPackageName();
                if (!Telephony.Sms.getDefaultSmsPackage(context).equals(myPackageName)) {
    
                    //Change the default sms app to my app
                    Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
                    intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, context.getPackageName());
                    startActivityForResult(intent, 1);
                }
            }
        });
    }
    
    //Write to the default sms app
    private void WriteSms(String message, String phoneNumber) {
    
        //Put content values
        ContentValues values = new ContentValues();
        values.put(Telephony.Sms.ADDRESS, phoneNumber);
        values.put(Telephony.Sms.DATE, System.currentTimeMillis());
        values.put(Telephony.Sms.BODY, message);
    
        //Insert the message
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            context.getContentResolver().insert(Telephony.Sms.Sent.CONTENT_URI, values);
        }
        else {
            context.getContentResolver().insert(Uri.parse("content://sms/sent"), values);
        }
    
        //Change my sms app to the last default sms
        Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
        intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, defaultSmsApp);
        context.startActivity(intent);
    }
    
    //Get result from default sms dialog pops up
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        if (requestCode == 1) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {
    
                final String myPackageName = getPackageName();
                if (Telephony.Sms.getDefaultSmsPackage(context).equals(myPackageName)) {
    
                    //Write to the default sms app
                    WriteSms(body, sender);
                }
            }
        }
    }
    

    As a result of adding things in your manifest you must add 4 classes: SmsReceiver, MmsReceiver, ComposeSmsActivity and HeadlessSmsSendService. You can let them empty as shown below.

    SmsReceiver:

    public class SmsReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
    
        }
    }
    

    MmsReceiver:

    public class MmsReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
    
        }
    }
    

    ComposeSmsActivity:

    public class ComposeSmsActivity extends ActionBarActivity {
    
    }
    

    HeadlessSmsSendService:

    public class HeadlessSmsSendService extends IntentService {
        public HeadlessSmsSendService() {
            super(HeadlessSmsSendService.class.getName());
        }
    
        @Override
        protected void onHandleIntent(Intent intent) {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    }
    

    If you need more help to understand this program have a look there:

    Youtube - DevBytes: Android 4.4 SMS APIs

    Android developers - Getting Your SMS Apps Ready for KitKat

    Possiblemobile - KitKat SMS and MMS supports

    0 讨论(0)
提交回复
热议问题