- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用此应用向我的手机发送消息,但不使用网络,但我的代码不起作用。我遵循了一些教程,检查了 android dev 并且我没有发现任何东西(在我的 logcat 中我没有错误)。你能帮我找出我的问题吗。
我的编译、编译器和手机信息:
Android Studio 1.0.1
API 19 安卓 4.4.4(奇巧)
构建 19
安卓手机版本4.4.4
list :
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
我的主要 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);
}
}
好吧,这就是我想做的,但使用我自己的应用程序而不是应用程序 Fake Text Message下载到 Play 商店。
制作假消息以及我应该在我的默认短信应用程序上看到什么:
最佳答案
在 Mike M 的帮助下,我终于完成了我的程序。因此,这是您必须添加到您的应用程序才能在不使用网络的情况下发送短信的代码:
list :
<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>
主要 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);
}
}
}
}
由于在 list 中添加内容,您必须添加 4 个类:SmsReceiver、MmsReceiver、ComposeSmsActivity 和 HeadlessSmsSendService。您可以让它们为空,如下所示。
短信接收者:
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) {
}
}
ComposeSmsActivity:
public class ComposeSmsActivity extends ActionBarActivity {
}
headless 短信发送服务:
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 SMS APIs
关于java - Android - 尝试在不使用移动网络的情况下向自己发送虚假短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28572168/
我试图弄清楚接受 OpenID 登录的网站如何无法通过简单的主机文件更新来指向伪造的 OpenID 提供商。 假设我想侵入 Joe Smith 的帐户,在这个例子中,假设他的 OpenID 提供商是
#include #include #include #include #include #include #include #include #include #include #define P
根据此讨论 - "RESTful API - Correct behavior when spurious/not requested parameters are passed in the req
如果编译为 Cand C++ 源代码,这个简单的代码片段会使用 g++ 4.7.0 生成“函数调用中缺少标记”警告。我相信这是编译器的错误,因为最终的 NULL值(value)就在那里。 #inclu
我读到,有时 && 运算符用于“短路”JavaScript,使其相信返回值 0 是 0 而不是 NaN,因为 0 在 JavaScript 中是一个虚假数字。我一直在四处寻找,想弄清楚这一切意味着什么
我正在使用 Borland(又名“Embarcodegearland”)C++Builder 2007 编译器,它有一个小错误,系统头文件中的某些 static const 项可能导致虚假的 "xyz
我是一名优秀的程序员,十分优秀!