当前位置: 首页 > 知识库问答 >
问题:

Android-尝试在没有移动网络使用的情况下给自己发送假短信

壤驷睿
2023-03-14

我试图用这个应用程序发送消息到我的手机,而不使用网络用法,但我的代码不工作。我遵循了一些教程,检查了android dev,我没有发现任何东西(在我的logcat中我没有错误)。你能帮我找出我的问题吗。

我关于编译、编译器和电话的信息:

>

  • Android Studio 1.0.1

    <uses-permission android:name="android.permission.WRITE_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>
    
    Context context;
    String sender;
    String body;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        //Get current context
        context = this;
    
        //App started
        Toast.makeText(context, "Started", Toast.LENGTH_LONG).show();
    
        CheckApp();
    }
    
    private void CheckApp() {
    
        sender = "1234";
        body = "Android sms body";
    
        //Get my package name
        final String myPackageName = getPackageName();
    
        //Check if my app is the default sms app
        if (!Telephony.Sms.getDefaultSmsPackage(this).equals(myPackageName)) {
    
            //Get default sms app
            String defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(context);
    
            //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());
            startActivity(intent);
    
            //Write the sms
            WriteSms(body, sender);
    
            //Change my sms app to the last default sms app
            Intent intent2 = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
            intent2.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, defaultSmsApp);
            startActivity(intent2);
        }
        else{
    
            //Write the sms
            WriteSms(body, sender);
        }
    }
    
    //Write the sms
    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);
        }
    }
    

    制作假消息,我应该在默认的短信应用程序上看到什么:

  • 共有1个答案

    欧阳英彦
    2023-03-14

    在迈克M的帮助下,我终于完成了我的程序。所以,这是你必须添加到你的应用程序中的代码,才能在不使用网络的情况下发送短信:

    舱单:

    <uses-permission android:name="android.permission.WRITE_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    
        <!-- BroadcastReceiver that listens for incoming SMS messages -->
        <receiver android:name=".SmsReceiver"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_DELIVER" />
            </intent-filter>
        </receiver>
    
        <!-- BroadcastReceiver that listens for incoming MMS messages -->
        <receiver android:name=".MmsReceiver"
            android:permission="android.permission.BROADCAST_WAP_PUSH">
            <intent-filter>
                <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
                <data android:mimeType="application/vnd.wap.mms-message" />
            </intent-filter>
        </receiver>
    
        <!-- My activity -->
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
        <!-- Activity that allows the user to send new SMS/MMS messages -->
        <activity android:name=".ComposeSmsActivity" >
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.SENDTO" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </activity>
    
        <!-- Service that delivers messages from the phone "quick response" -->
        <service android:name=".HeadlessSmsSendService"
            android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </service>
    
    </application>
    

    主要活动:

    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);
                }
            }
        }
    }
    
    public class SmsReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
    
        }
    }
    
    public class MmsReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
    
        }
    }
    
    public class ComposeSmsActivity extends ActionBarActivity {
    
    }
    
    public class HeadlessSmsSendService extends IntentService {
        public HeadlessSmsSendService() {
            super(HeadlessSmsSendService.class.getName());
        }
    
        @Override
        protected void onHandleIntent(Intent intent) {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    }
    

    YouTube-DevBytes:Android 4.4短信API

    Android开发人员-为KitKat准备好您的短信应用程序

    PossibleMobile-KitKat SMS和MMS支持

     类似资料:
    • 我看到的所有解决方案都需要使用。但是,我想在Eclipse之外的单个文件上使用CDT解析器。那有什么办法吗?

    • 我的用例是通过AWS SNS向印度移动发送短信。我创建了用户并选择了短信协议。对于endpoint,我给出了我的手机号码——它接受并创建了订阅。 我为订阅创建了向我的手机号码发送短信的主题。它显示已发送的消息,但我没有收到任何来自该主题的消息? 如何在 AWS SNS 中向印度手机号码发送短信? 请帮帮我!!!预先感谢...

    • 问题内容: 我看到了一些这样的代码: 我以为应该有一个? 为什么这段代码这样做呢? 问题答案: 如果您希望当前执行的方法仍引发异常,同时允许适当地清理资源,则这很有用。下面是处理调用方法中的异常的具体示例。

    • 问题内容: 我需要使用浏览整个页面。 现在我正在做: 当我单击Link1时,此方法工作正常,页面滚动到ID为“ div1”的div。 关键是,我不想更改单击后的后缀为URL的URL 。 我尝试了锚href 和 如何避免更改URL? 问题答案: 从Jeff Hines的jQuery动画中获得以下答案: 如果您使用的是jQuery,请不要忘记将库添加到项目中。 编辑:另外,请确保您仍然“返回false

    • 我是UI自动测试的新手,当只有.apk文件时,我无法弄清楚如何设置UI测试。 在线教程和其他示例没有显示如何使用我的3P. apk文件。我知道在Appium中,只需将文件/目录和名称添加到所需的功能,服务器就会安装它。