- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我已经弄清楚如何发送和接收 SMS 消息。要发送 SMS 消息,我必须调用 sendTextMessage()
和 sendMultipartTextMessage()
SmsManager
的方法类(class)。要接收 SMS 消息,我必须在 AndroidMainfest.xml
中注册一个接收器。文件。然后我不得不覆盖 onReceive()
BroadcastReceiver
的方法.我在下面提供了示例。
MainActivity.java
public class MainActivity extends Activity {
private static String SENT = "SMS_SENT";
private static String DELIVERED = "SMS_DELIVERED";
private static int MAX_SMS_MESSAGE_LENGTH = 160;
// ---sends an SMS message to another device---
public static void sendSMS(String phoneNumber, String message) {
PendingIntent piSent = PendingIntent.getBroadcast(mContext, 0, new Intent(SENT), 0);
PendingIntent piDelivered = PendingIntent.getBroadcast(mContext, 0,new Intent(DELIVERED), 0);
SmsManager smsManager = SmsManager.getDefault();
int length = message.length();
if(length > MAX_SMS_MESSAGE_LENGTH) {
ArrayList<String> messagelist = smsManager.divideMessage(message);
smsManager.sendMultipartTextMessage(phoneNumber, null, messagelist, null, null);
}
else
smsManager.sendTextMessage(phoneNumber, null, message, piSent, piDelivered);
}
}
//More methods of MainActivity ...
}
SMSReceiver.java
public class SMSReceiver extends BroadcastReceiver {
private final String DEBUG_TAG = getClass().getSimpleName().toString();
private static final String ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private Context mContext;
private Intent mIntent;
// Retrieve SMS
public void onReceive(Context context, Intent intent) {
mContext = context;
mIntent = intent;
String action = intent.getAction();
if(action.equals(ACTION_SMS_RECEIVED)){
String address, str = "";
int contactId = -1;
SmsMessage[] msgs = getMessagesFromIntent(mIntent);
if (msgs != null) {
for (int i = 0; i < msgs.length; i++) {
address = msgs[i].getOriginatingAddress();
contactId = ContactsUtils.getContactId(mContext, address, "address");
str += msgs[i].getMessageBody().toString();
str += "\n";
}
}
if(contactId != -1){
showNotification(contactId, str);
}
// ---send a broadcast intent to update the SMS received in the
// activity---
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
context.sendBroadcast(broadcastIntent);
}
}
public static SmsMessage[] getMessagesFromIntent(Intent intent) {
Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
byte[][] pduObjs = new byte[messages.length][];
for (int i = 0; i < messages.length; i++) {
pduObjs[i] = (byte[]) messages[i];
}
byte[][] pdus = new byte[pduObjs.length][];
int pduCount = pdus.length;
SmsMessage[] msgs = new SmsMessage[pduCount];
for (int i = 0; i < pduCount; i++) {
pdus[i] = pduObjs[i];
msgs[i] = SmsMessage.createFromPdu(pdus[i]);
}
return msgs;
}
/**
* The notification is the icon and associated expanded entry in the status
* bar.
*/
protected void showNotification(int contactId, String message) {
//Display notification...
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />
<uses-permission android:name="android.permission.WRITE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:debuggable="true"
android:icon="@drawable/ic_launcher_icon"
android:label="@string/app_name" >
<activity
//Main activity...
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
//Activity 2 ...
</activity>
//More acitivies ...
// SMS Receiver
<receiver android:name="com.myexample.receivers.SMSReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
但是,我想知道您是否可以以类似的方式发送和接收彩信。在做了一些研究后,博客上提供的许多示例只是通过 Intent
到 native 消息传递应用程序。我正在尝试在不离开我的应用程序的情况下发送彩信。似乎没有发送和接收彩信的标准方式。有人让这个工作吗?
另外,我知道 SMS/MMS ContentProvider 不是官方 Android SDK 的一部分,但我想可能有人能够实现这一点。非常感谢任何帮助。
我添加了一个 BroadcastReceiver
到AndroidManifest.xml
接收彩信的文件
<receiver android:name="com.sendit.receivers.MMSReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
在 MMSReceiver 类中,onReceive()
方法只能获取发送消息的电话号码。您如何从 MMS 中获取其他重要信息,例如媒体附件(图像/音频/视频)的文件路径或 MMS 中的文本?
MMSReceiver.java
public class MMSReceiver extends BroadcastReceiver {
private final String DEBUG_TAG = getClass().getSimpleName().toString();
private static final String ACTION_MMS_RECEIVED = "android.provider.Telephony.WAP_PUSH_RECEIVED";
private static final String MMS_DATA_TYPE = "application/vnd.wap.mms-message";
// Retrieve MMS
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String type = intent.getType();
if(action.equals(ACTION_MMS_RECEIVED) && type.equals(MMS_DATA_TYPE)){
Bundle bundle = intent.getExtras();
Log.d(DEBUG_TAG, "bundle " + bundle);
SmsMessage[] msgs = null;
String str = "";
int contactId = -1;
String address;
if (bundle != null) {
byte[] buffer = bundle.getByteArray("data");
Log.d(DEBUG_TAG, "buffer " + buffer);
String incomingNumber = new String(buffer);
int indx = incomingNumber.indexOf("/TYPE");
if(indx>0 && (indx-15)>0){
int newIndx = indx - 15;
incomingNumber = incomingNumber.substring(newIndx, indx);
indx = incomingNumber.indexOf("+");
if(indx>0){
incomingNumber = incomingNumber.substring(indx);
Log.d(DEBUG_TAG, "Mobile Number: " + incomingNumber);
}
}
int transactionId = bundle.getInt("transactionId");
Log.d(DEBUG_TAG, "transactionId " + transactionId);
int pduType = bundle.getInt("pduType");
Log.d(DEBUG_TAG, "pduType " + pduType);
byte[] buffer2 = bundle.getByteArray("header");
String header = new String(buffer2);
Log.d(DEBUG_TAG, "header " + header);
if(contactId != -1){
showNotification(contactId, str);
}
// ---send a broadcast intent to update the MMS received in the
// activity---
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("MMS_RECEIVED_ACTION");
broadcastIntent.putExtra("mms", str);
context.sendBroadcast(broadcastIntent);
}
}
}
/**
* The notification is the icon and associated expanded entry in the status
* bar.
*/
protected void showNotification(int contactId, String message) {
//Display notification...
}
}
根据Documentation of android.provider.Telephony :
Broadcast Action: A new text based SMS message has been received by the device. The intent will have the following extra values:
pdus
- AnObject[]
ofbyte[]
s containing the PDUs that make up the message.The extra values can be extracted using
getMessagesFromIntent(android.content.Intent)
If a BroadcastReceiver encounters an error while processing this intent it should set the result code appropriately.@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
Broadcast Action: A new data based SMS message has been received by the device. The intent will have the following extra values:
pdus
- AnObject[]
ofbyte[]
s containing the PDUs that make up the message.The extra values can be extracted using getMessagesFromIntent(android.content.Intent). If a BroadcastReceiver encounters an error while processing this intent it should set the result code appropriately.
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String DATA_SMS_RECEIVED_ACTION = "android.intent.action.DATA_SMS_RECEIVED";
Broadcast Action: A new WAP PUSH message has been received by the device. The intent will have the following extra values:
transactionId (Integer)
- The WAP transaction ID
pduType (Integer)
- The WAP PDU type`
header (byte[])
- The header of the message
data (byte[])
- The data payload of the message
contentTypeParameters (HashMap<String,String>)
- Any parameters associated with the content type (decoded from the WSP Content-Type header)If a BroadcastReceiver encounters an error while processing this intent it should set the result code appropriately. The contentTypeParameters extra value is map of content parameters keyed by their names. If any unassigned well-known parameters are encountered, the key of the map will be 'unassigned/0x...', where '...' is the hex value of the unassigned parameter. If a parameter has No-Value the value in the map will be null.
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String WAP_PUSH_RECEIVED_ACTION = "android.provider.Telephony.WAP_PUSH_RECEIVED";
我已经知道如何在 PendingIntent
中传递额外内容由 BroadcastReceiver
接收: Android PendingIntent extras, not received by BroadcastReceiver
但是,额外的被传递给 SendBroadcastReceiver 而不是 SMSReceiver。我怎样才能将额外信息传递给 SMSReceiver?
接收彩信
所以在做了更多研究之后,我看到了一些注册 ContentObserver
的建议。 .这样您就可以检测到 content://mms-sms/conversations
何时有任何更改。内容提供程序,因此允许您检测传入的彩信。这是我发现的最接近的示例:Receiving MMS
但是,有一个变量 mainActivity
类型 ServiceController
. ServiceController
在哪里类实现?是否有注册的ContentObserver
的任何其他实现? ?
发送彩信
关于发送彩信,我遇到过这个例子:Send MMS
问题是我尝试在 Android v4.2.2 上的 Nexus 4 上运行此代码,但收到此错误:
java.lang.SecurityException: No permission to write APN settings: Neither user 10099 nor current process has android.permission.WRITE_APN_SETTINGS.
查询 Carriers
后抛出错误getMMSApns()
中的 ContentProvider APNHelper
的方法类。
final Cursor apnCursor = this.context.getContentResolver().query(Uri.withAppendedPath(Carriers.CONTENT_URI, "current"), null, null, null, null);
对于所有使用移动数据执行操作(如发送彩信)但不知道设备中存在的默认 APN 设置的应用程序,有什么替代方案?
发送彩信
我尝试过以下示例:Send MMS
正如@Sam 在他的回答中建议的那样:
You have to add jsoup to the build path, the jar to the build path and import com.droidprism.*; To do that in android, add the jars to the libs directory first, then configure the project build path to use the jars already in the libs directory, then on the build path config click order and export and check the boxes of the jars and move jsoup and droidprism jar to the top of the build order.
所以现在我不再收到 SecurityException 错误。我现在正在 Android KitKat 上的 Nexus 5 上进行测试。运行示例代码后,它会在调用
后给我一个 200 响应代码MMResponse mmResponse = sender.send(out, isProxySet, MMSProxy, MMSPort);
但是,我与尝试向其发送彩信的人进行了核实。他们说他们从未收到过彩信。
最佳答案
我遇到了与您在上面描述的完全相同的问题(t-mobile USA 上的 Galaxy Nexus),这是因为移动数据已关闭。
在果冻 bean 中是:设置 > 数据使用 > 移动数据
请注意,在发送彩信或接收彩信之前,我必须打开移动数据。如果我收到关闭移动数据的彩信,我将收到一条新消息的通知,我将收到带有下载按钮的消息。但是如果我之前没有打开移动数据,则不会收到传入的彩信附件。即使我收到消息后打开它。
由于某些原因,当您的电话提供商允许您发送和接收彩信时,您必须启用移动数据,即使您使用的是 Wifi,如果启用了移动数据,您将能够接收和发送彩信,即使 Wifi 在您的设备上显示为您的互联网。
这真的很痛苦,就像你没有打开它一样,即使打开移动数据,消息也会挂很多,并且可能需要重新启动设备。
关于android - 在 Android 中发送和接收短信和彩信(Kit Kat Android 4.4 之前),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14452808/
我没有使用 Sprite Kit 的经验。我想知道是否有类似 Cocos2D 的东西 调度程序在 SpriteKit 中?如果不是,应该用什么 NSTimer 是唯一的选择吗?我想如果唯一的选择是使用
在我的 Sprite Kit 应用程序中,我想要一个粘性物理体:任何与该物体接触的动态物体都不会反弹,而是与其保持连接。 是否有任何属性可以执行这种粘性行为?如果不能,如何实现? 最佳答案 SKNod
在我的 Sprite Kit 应用程序中,我想要一个粘性物理体:任何与该物体接触的动态物体都不会反弹,而是与其保持连接。 是否有任何属性可以执行这种粘性行为?如果不能,如何实现? 最佳答案 SKNod
我正在构建一个 flappy bird 游戏,其中一半关卡在水上,另一半关卡在水下。 如果您的小鸟在空中,则存在规律的重力,当您轻敲时,会施加一个直线向上的冲量。 如果你的鸟在水中,重力应该在负方向(
我正在使用 Sprite 套件制作游戏,现在我想知道让对象“跳跃”的最佳方法是什么。所以它将以几个像素垂直启动。 这是我要跳转的对象的代码: SKSpriteNode *sprite = [SKSpr
我很好奇如何为我的游戏显示像素艺术。现在我只是将 SKScene 的大小调整为 sceneWithSize:CGSizeMake(256, 192) 这是正确的方法还是有更好的方法来进行这种排序任务?
我不知道如何阻止节点旋转。 为什么允许旋转没有禁用它? 这是我描述我的节点的方式: SKSpriteNode *badguy = [SKSpriteNode spriteNodeWithTexture
我在 Xcode 中使用 Sprite Kit,我想知道如何更改重力方向。默认重力方向为“X”,您可以在轴下方的图形上想象。如果我想更改为“Y”怎么办。 我的目标是给予物体坠落效果。它就像从高处坠落并
我想知道如何在屏幕外删除我的 SKNode 以帮助我的游戏运行更流畅。 如何在 Sprite Kit 上做到这一点 非常感谢 最佳答案 https://stackoverflow.com/a/2419
我有一个关于“WEBKIT”的问题。什么是 WebKit?什么是 Android WebKit 以及它与 IOS WEbKit 有何不同?如何使用 WebKit?想要完美清晰的答案。有人有什么想法吗?
在我的 Sprite 套件游戏中,当两个 Sprite 发生碰撞时,我使用 SKAction 播放 mp3 文件。虽然游戏运行良好,但我在第一次播放声音时注意到游戏中有一个小的“hickup”。整个游
我正在创建一个游戏,对于我的角色,他的腿和躯干有单独的动画,因此它们是具有单独物理体的单独节点。我在将躯干和腿连接在一起时遇到了很多麻烦,但是,将它们连接起来不是问题,让它们保持连接才是问题。在四处走
Sprite Kit 是否具有相当于 9 切片缩放的 Sprite ? 我试过谷歌搜索,但没有找到任何东西,但是......它通常是一个在不同框架中有不同名称的功能。所以我可能会错过它。 最佳答案 S
我正在 Sprite Kit 中制作“Achtung die kurve”克隆。对于不断移动的队伍/球员,我使用 CGMutablePathRef 和 SKShapeNode。在更新方法中我正在这样做
我正在尝试开发一个building information model iPad 的查看器,我面临着当前的挑战。我应该使用 SpriteKit 还是 SceneKit?我知道 SceneKit 是用来
在 iPhone ios10 上安装后,我的 ready to ship appstore 应用程序不再播放声音!帮助!使用 ios9,一切都完美运行 SKAction.playSoundFileNa
如何将 CIPixellate 核心图像过滤器添加到 Sprite Kit 场景中? 我有一个 SpriteKit 场景,它是 SKScene 或其子类。 我想在场景中添加一个核心图像过滤器。特别是
这似乎不是一个明智的问题,但我心中有一些困惑,我想澄清。在Apple的2d游戏框架Sprite kit中,通常建议将节点相关的数据存储在Plist中,然后在代码中需要时检索。我不明白的是,在使用 .s
这似乎不是一个明智的问题,但我心中有一些困惑,我想澄清。在Apple的2d游戏框架Sprite kit中,通常建议将节点相关的数据存储在Plist中,然后在代码中需要时检索。我不明白的是,在使用 .s
我试图让用户能够将 skspritenode 拖到 UIImageView 和 UIButton 上,但每当我创建场景时,它都会出现在 UI 元素后面。我该如何解决这个问题? 最佳答案 恐怕这是不可能
我是一名优秀的程序员,十分优秀!