java Android - 尝试在不使用移动网络的情况下向自己发送假短信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28572168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:50:44  来源:igfitidea点击:

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

javaandroid

提问by Albentrix

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 found anything (in my logcat I don't have error). Could you help me to find out my problem.

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

My information about compilation, compiler and phone:

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

  • Android Studio 1.0.1

  • API 19 Android 4.4.4 (kitkat)

  • Build 19

  • Android phone version 4.4.4

  • 安卓工作室 1.0.1

  • API 19 Android 4.4.4 (kitkat)

  • 建造 19

  • 安卓手机版本 4.4.4

Manifest:

显现:

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

Function of my main activity:

我的主要活动的功能:

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);
    }
}

Well, this is what i wanna do but with my own app and not with the app Fake Text Messagethat downloaded to the play store.

嗯,这就是我想要做的,但使用我自己的应用程序,而不是使用下载到 Play 商店的应用程序假短信

Make the fake message and what should i see on my default sms app:

制作虚假信息以及我应该在我的默认短信应用上看到什么:

采纳答案by Albentrix

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:

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

Manifest:

显现:

<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>

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.

作为在清单中添加内容的结果,您必须添加 4 个类:SmsReceiver、MmsReceiver、ComposeSmsActivity 和 HeadlessSmsSendService。您可以让它们清空,如下所示。

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:

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

Youtube - DevBytes:Android 4.4 SMS API

Android developers - Getting Your SMS Apps Ready for KitKat

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

Possiblemobile - KitKat SMS and MMS supports

可能的移动 - KitKat SMS 和 MMS 支持