- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试在我的应用中接收短信。
我的项目中有一个 BroadcastReceiver 类,但我收到一个运行时错误:
System.InvalidCastException: Cannot cast from source type to destination type. at at (wrapper castclass) object.__castclass_with_cache (object,intptr,intptr) at at Android.Runtime.JNIEnv.CopyArray (intptr,Java.Lang.Object[]) <0x002a3> at at Android.Runtime.JNIEnv.GetArray (intptr) <0x0021f>
at Messages.SMSBroadcastReceiver.OnReceive (Android.Content.Context,Android.Content.Intent) [0x0005f] in c:\Users\Jase\Documents\Projects\Messages\Messages\SMSBroadcastReceiver.cs:36 at Android.Content.BroadcastReceiver.n_OnReceive_Landroid_content_Context_Landroid_content_Intent_ (intptr,intptr,intptr,intptr) [0x00019] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Content.BroadcastReceiver.cs:334 at at (wrapper dynamic-method) object.145a0c82-0de6-4c2c-90a0-3654436a06c3 (intptr,intptr,intptr,intptr)
这是我正在使用的代码:
using System;
using System.Text;
using Android.App;
using Android.OS;
using Android.Content;
using Android.Runtime;
using Android.Util;
using Android.Widget;
using Android.Telephony;
using Environment = System.Environment;
namespace Messages
{
[BroadcastReceiver(Enabled = true, Label = "SMS Receiver")]
[IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" })]
public class SMSBroadcastReceiver : BroadcastReceiver
{
private const string Tag = "SMSBroadcastReceiver";
private const string IntentAction = "android.provider.Telephony.SMS_RECEIVED";
public override void OnReceive(Context context, Intent intent)
{
Log.Info(Tag, "Intent: " + intent.Action);
if (intent.Action != IntentAction)
return;
var bundle = intent.Extras;
if (bundle == null)
return;
var pdus = bundle.Get("pdus");
// ********** This is the error line at runtime ****************
var castedPdus = JNIEnv.GetArray<Java.Lang.Object>(pdus.Handle);
var messages = new SmsMessage[castedPdus.Length];
var stringBuilder = new StringBuilder();
for (int i = 0; i < messages.Length; i++)
{
var bytes = new byte[JNIEnv.GetArrayLength(castedPdus[i].Handle)];
JNIEnv.CopyArray(castedPdus[i].Handle, bytes);
messages[i] = SmsMessage.CreateFromPdu(bytes);
stringBuilder.Append(String.Format("SMS from: {0}{1}Body: {2}{1}", messages[i].OriginatingAddress,
Environment.NewLine, messages[i].MessageBody));
}
Toast.MakeText(context, stringBuilder.ToString(), ToastLength.Long).Show();
}
}
}
一段时间以来,我一直在努力想出一个解决方案,并且已经搜索了几个小时,但针对此错误的现有解决方案都没有奏效。我不明白。有人可以帮助阐明这一点吗?
我正在更新这个问题:
Bundle not null: Bundle[mParcelledData.dataSize=256]
PDUS: [[B@5s294833
PDUS Handle: 2065454
Cannot cast from source type to destination type. at (wrapper castclass) object:__castclass_with_cache (object,intptr,intptr)
at Android.Runtime.JNIEnv.CopyArray[Object] (IntPtr src, Java.Lang.Object[] dest) [0x00078] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.cs:946
at Android.Runtime.JNIEnv.GetArray[Object] (IntPtr array_ptr) [0x00053] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.cs:1211
at Java.Lang.Object.ToArray[Object] () [0x00000] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/src/Java.Lang/Object.cs:338
at Java.Lang.Object.op_Explicit (Java.Lang.Object value) [0x00008] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/src/Java.Lang/Object.cs:499
at Messages.SMSBroadcastReceiver.OnReceive (Android.Content.Context context, Android.Content.Intent intent) [0x000ba] in c:\Users\Jase\Documents\Projects\Messages\Messages\SMSBroadcastReceiver.cs:52
我还想指出,我已经尝试了很多方法来解决这个问题。我现在(真的)在 Google 搜索结果的第 60 页上,试图找到东西。问题是,我在网上找到的解决这个完全相同的错误的解决方案都没有真正起作用,因为它们基本上只是同一语句的微小变化,只是以稍微不同的方式编写但做同样的事情会产生相同的异常.
我也试过以下方法:
// var castedPdus = JNIEnv.GetObjectArrayElement(pdus.Handle, 0);
// Object castedPdus = (Object)bundle.Get("pdus");
// var castedPdus = JNIEnv.GetArray<Java.Lang.Object>(pdus.Handle);
没有任何运气。
根据我使用的语句,异常从“无法从源类型转换为目标类型”到“无法将对象 [] 转换为对象”不等。好吧,呃。但问题不在于我“做错了”,而是我们被告知“做错了”。我无法在网上找到其他在 Xamarin 中接收 SMS 的方法。在线的每个示例似乎都是基于这个代码 fragment - 进行一些小的改动。我不知道现在该怎么办。似乎连一丝希望都没有。
最佳答案
由于您说您将在 Android Studio 中使用 Java,但没有留下答案,所以我在这里留一个答案给您和可能正在寻找解决方案的任何其他人。
短信接收者.java:
package com.your.app;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SMS_RECEIVED)) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
// get sms objects
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus.length == 0) {
return;
}
// large message might be broken into many
SmsMessage[] messages = new SmsMessage[pdus.length];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
sb.append(messages[i].getMessageBody());
}
String sender = messages[0].getOriginatingAddress();
Log.d("SNDR", sender);
String message = sb.toString();
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
// prevent any other broadcast receivers from receiving broadcast
abortBroadcast();
}
}
}
}
请务必将所需的权限和 intent-filter 添加到您的 AndroidManifest.xml 文件中。
AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<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>
<receiver android:name="com.your.app.SmsReceiver" android:enabled="true">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
关于java - 无法从源类型转换为目标类型 (JNIEnv.GetArray<Java.Lang.Object>(pudis.Handle);),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27544145/
我创建了一个基于命令行可移植脚本的工业化不可知构建系统,可用于快速构建多个依赖项目,而不必依赖特定的 IDE 或构建工厂。它是不可知的,因为它不是基于单个构建引擎。我使用 cmake 创建了第一个版本
我最初使用 Java 目标开发了一个语法(用于 TestRig 支持),然后将其移植到 Python(从 git hub 语法存储库扩展了 Python3 语法,因此需要将操作移植到 Python
我有一个以 iPhone 和 watchOS 为目标的 Xcode 项目。 iPhone 目标使用加速度计,模拟器不支持。我可以只启动 iPhone 应用程序而不启动 watch 目标吗?我从: Ca
您好,我想创建一个批处理文件,用于在 .eml 文件(目标 A)中查找某些关键字,然后删除它们所在的行。之后,我需要批处理文件将"new"文件放入(目标 B)中的单独 .eml 文件中。文件也可以是
当尝试通过 IntelliJ 运行示例 CorDapp (GitHub CorDapp) 时,我收到以下错误: Cannot inline bytecode built with JVM target
我在尝试向我的 kotlin spring 项目添加一些依赖项时遇到问题。我使用 spring boot 初始化程序来运行一个基本项目。 我的问题:如果我取消对 jackson 或 Koin 依赖项的
这是有问题的网站: http://www.onepixelroom.com/londonrefurb 当我点击关于部分后面的多个圆圈时,我希望它更改上面文本中的引号。 到目前为止,我得到它来显示 文本
单击后,我将删除两个元素 $(this) 和 $("#foo")。 目前我的代码如下所示: $(this).remove(); $("#foo").remove(); 如何在不重复自己的情况下优化它?
我有一个小脚本,可将 Markdown 文件编译为 html,并将其与一些样式表和 javascript 一起插入到模板的主体中。我有一个 GNU makefile 来完成这个: output.htm
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
一些背景知识: 在android中我们开发了同样的应用,基本上我们先开发了Android应用,现在我们创建了它的IOS版本,所以这个应用有多个客户端。在 android 中,我们实际上是使用 Andr
我想知道是否可以使用 knockout 来更改html中的目标() 我的所有其他信息都在 JavaScript 中,所以这对我来说是一个大问题。这是我的 JavaScript: var library
这个问题在这里已经有了答案: Selecting and manipulating CSS pseudo-elements such as ::before and ::after using j
我在我的有向图中添加了一堆节点和顶点,使用设置 typedef boost::adjacency_list graph; 创建 Node有一个节点名称字符串,Edge它的分数有一个整数。我试图遍历所有
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 8 年前。 Improve
如何存储我在 NSUserDefaults 中创建的 Goal 类型的对象数组? ( swift ) 代码如下: func saveGoalList ( newGoalList : [Goal] ){
Array.prototype.indexOf 和 Date.now 已在 ES5 中引入。如果我编译存储在文件 test.ts 中的以下代码,为什么 Typescript 不能转译? Date.no
我正在阅读有关属性的内容,并了解到可以使用您的代码将它们应用于不同的目标实体 -(请参阅 Attribute Targets)。 因此,查看我项目中的 AssemblyInfo.cs 文件,我可以看到
给定一个 Makefile: all: build/a build/b build/c # need to change this to all: build/* build/a:
我有一个带有多框架目标的项目- netstandard2.0;net471 . 我想为 netframework 构建解决方案和 netstandard分别。 目前我使用这个 MSBuild 命令:
我是一名优秀的程序员,十分优秀!