- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我构建了一个 SMS 发送/接收 Activity ,一切正常。接收部分只是将我的所有文本从我的 Android 消息传递应用程序加载到 ListView
中。这也行。问题是当收到新短信时,它会立即崩溃。即使我在另一个应用程序或主屏幕上,我也会收到一个弹出窗口,提示我的应用程序已崩溃,并且每次收到新消息时都会重新崩溃。为什么会崩溃,我该如何解决?包含消息的 ListView
应该在收到新消息时更新。我将这段代码拼凑在一起,所以很可能我遗漏了一些东西。此外,只关注 SMSReceiver
类(在 SMSListFragment.java
中)和 refreshSmsInbox()
(在 SMSMain.java
中>),因为这些地方是加载到我的 ListView 的地方。此外,updateList()
会更新整个过程,但前提是我的收件箱中有预先存在的邮件。如果更新实际上可以实时更新,那就太好了。那可能吗?提前致谢。
在我的 list 中,这是我的相关权限:
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<receiver android:name=".SMSReceiver"
android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
SMSMain.java
package org.azurespot.practiceapp.sms;
import java.util.ArrayList;
import org.azurespot.practiceapp.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.speech.RecognizerIntent;
import android.telephony.SmsManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class SMSMain extends Activity {
protected EditText etPhoneNumber;
protected static EditText etMessage;
protected View dialogView;
private static final int CONTACT_PICKER_RESULT = 1001;
AlertDialog dialog;
protected static final int RESULT_SPEECH = 1;
private static SMSMain smsMain;
ArrayList<String> smsMessagesList = new ArrayList<String>();
ListView smsListView;
ArrayAdapter<String> arrayAdapter;
public static SMSMain instance() {
return smsMain;
}
@Override
public void onStart() {
super.onStart();
smsMain = this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smsmain);
// initialize ListView for displaying SMS messages
smsListView = (ListView) findViewById(R.id.messages_list);
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, smsMessagesList);
smsListView.setAdapter(arrayAdapter);
// long click on ListView item will delete message
smsListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int pos, long arg3) {
//removes item at position you long-click on
smsMessagesList.remove(pos);//where arg2 is position of item you click
arrayAdapter.notifyDataSetChanged();
return true;
}
});
} // end onCreate
public void readSMS(View v){
refreshSmsInbox();
}
// adds text messages to the ListView via the adapter
public void refreshSmsInbox() {
ContentResolver contentResolver = getContentResolver();
Cursor smsInboxCursor = contentResolver.query(Uri.parse
("content://sms/inbox"), null, null, null, null);
int indexBody = smsInboxCursor.getColumnIndex("body");
int indexAddress = smsInboxCursor.getColumnIndex("address");
if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
arrayAdapter.clear();
do {
String str = "SMS From: " + smsInboxCursor.getString(indexAddress) +
"\n" + smsInboxCursor.getString(indexBody) + "\n";
arrayAdapter.add(str);
} while (smsInboxCursor.moveToNext());
}
public void updateList(final String smsMessage) {
arrayAdapter.insert(smsMessage, 0);
arrayAdapter.notifyDataSetChanged();
}
public void composeSMS(View v){
// build your dialog box
AlertDialog.Builder builder = new AlertDialog.Builder(SMSMain.this);
// Get the layout inflater & inflate the box
final LayoutInflater inflater = SMSMain.this.getLayoutInflater();
dialogView = inflater.inflate(R.layout.dialog_compose_sms, null);
builder.setView(dialogView);
// get phone number and message IDs
etPhoneNumber = (EditText)dialogView.findViewById(R.id.sms_phone);
etMessage = (EditText) dialogView.findViewById(R.id.sms_message);
builder.setTitle("Send an SMS");
builder.setPositiveButton("Send", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Turns the entered phone number and message into String
String phoneNumber = etPhoneNumber.getText().toString().trim();
String message = etMessage.getText().toString().trim();
// error check to make sure there is input
if (phoneNumber.length() > 0 && message.length() > 0)
// sends the SMS
sendSMS(phoneNumber, message);
else
Toast.makeText(getBaseContext(),
"Please enter a phone number and message.",
Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
dialog.cancel();
}
});
// put all your builders in one create command
dialog = builder.create();
// show the dialog box
dialog.show();
}
// clicked the microphone button to start speech to text
public void speechToText(View v) {
Intent intent = new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
etMessage.setText("");
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"Ooopps! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
}
// method for sending the SMS
private void sendSMS(String phoneNumber, String message)
{
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, SMSMain.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, pi, null);
}
public void addContact(View v) {
// add a contact. using Android's ContactPicker
Intent i = new Intent(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(i, CONTACT_PICKER_RESULT);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_SPEECH){
// speech to text result
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra
(RecognizerIntent.EXTRA_RESULTS);
etMessage.setText(text.get(0));
}
break;
}
}
} // end if
// picks a contact, puts into EditText
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
String phone = "";
try{
Uri result = data.getData();
// get the contact id from the URI
String id = result.getLastPathSegment();
// query for phone number
cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone._ID + "=?",
new String[]{id}, null);
int phoneIdx = cursor.getColumnIndex(Phone.DATA);
if (cursor.moveToFirst()) {
phone = cursor.getString(phoneIdx);
}
} catch (Exception e) {
System.out.println("Getting contact failed.");
} finally {
etPhoneNumber.setText(phone, TextView.BufferType.EDITABLE);
if (phone.length() == 0) {
Toast.makeText(this, "No phone number found for contact.",
Toast.LENGTH_LONG).show();
}
if (cursor != null){
cursor.close();
}
} // end finally
break;
}
}
}
}
SMSListFragment.java
package org.azurespot.practiceapp.sms;
import org.azurespot.practiceapp.R;
import android.app.ListFragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.SmsMessage;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class SMSListFragment extends ListFragment{
public static View viewList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Defines the XML file for the fragment
viewList = inflater.inflate(R.layout.sms_messages_fragment,
container, false);
return viewList;
}
public class SMSReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] messages = null;
String msgString = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
messages = new SmsMessage[pdus.length];
for (int i = 0; i < messages.length; i++){
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
msgString += "SMS From: " + messages[i].getOriginatingAddress();
msgString += " :";
msgString += messages[i].getMessageBody().toString();
msgString += "\n";
}
//---display the new SMS message---
SMSMain smsMain = SMSMain.instance();
smsMain.updateList(msgString);
}
}
}
}
逻辑猫
01-01 18:24:09.444: E/AndroidRuntime(13141): FATAL EXCEPTION: main
01-01 18:24:09.444: E/AndroidRuntime(13141): Process: org.azurespot.practiceapp, PID: 13141
01-01 18:24:09.444: E/AndroidRuntime(13141): java.lang.RuntimeException: Unable to instantiate receiver org.azurespot.practiceapp.SMSReceiver: java.lang.ClassNotFoundException: Didn't find class "org.azurespot.practiceapp.SMSReceiver" on path: DexPathList[[zip file "/data/app/org.azurespot.practiceapp-2.apk"],nativeLibraryDirectories=[/data/app-lib/org.azurespot.practiceapp-2, /vendor/lib, /system/lib]]
01-01 18:24:09.444: E/AndroidRuntime(13141): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2608)
01-01 18:24:09.444: E/AndroidRuntime(13141): at android.app.ActivityThread.access$1800(ActivityThread.java:173)
01-01 18:24:09.444: E/AndroidRuntime(13141): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1379)
01-01 18:24:09.444: E/AndroidRuntime(13141): at android.os.Handler.dispatchMessage(Handler.java:102)
01-01 18:24:09.444: E/AndroidRuntime(13141): at android.os.Looper.loop(Looper.java:136)
01-01 18:24:09.444: E/AndroidRuntime(13141): at android.app.ActivityThread.main(ActivityThread.java:5579)
01-01 18:24:09.444: E/AndroidRuntime(13141): at java.lang.reflect.Method.invokeNative(Native Method)
01-01 18:24:09.444: E/AndroidRuntime(13141): at java.lang.reflect.Method.invoke(Method.java:515)
01-01 18:24:09.444: E/AndroidRuntime(13141): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
01-01 18:24:09.444: E/AndroidRuntime(13141): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
01-01 18:24:09.444: E/AndroidRuntime(13141): at dalvik.system.NativeStart.main(Native Method)
01-01 18:24:09.444: E/AndroidRuntime(13141): Caused by: java.lang.ClassNotFoundException: Didn't find class "org.azurespot.practiceapp.SMSReceiver" on path: DexPathList[[zip file "/data/app/org.azurespot.practiceapp-2.apk"],nativeLibraryDirectories=[/data/app-lib/org.azurespot.practiceapp-2, /vendor/lib, /system/lib]]
01-01 18:24:09.444: E/AndroidRuntime(13141): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
01-01 18:24:09.444: E/AndroidRuntime(13141): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
01-01 18:24:09.444: E/AndroidRuntime(13141): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
01-01 18:24:09.444: E/AndroidRuntime(13141): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2603)
01-01 18:24:09.444: E/AndroidRuntime(13141): ... 10 more
最佳答案
当您在 list 中声明接收者时:
<receiver android:name=".SMSReceiver"
android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
android:name=".SMSReceiver"
中的 .
将扩展为您的包名称。因此,您在 list 中所写的内容等同于
<receiver android:name="org.azurespot.practiceapp.SMSReceiver"
android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
请注意,此路径与您在 SMSListFragment
中定义的类 SMSReceiver
的路径不匹配,这是 logcat 错误指示的内容。
您可能应该在 list 中使用 android:name=".sms.SMSListFragment.SMSReceiver"
。 SMSReceiver
嵌套类可能也需要声明为静态才能以这种方式访问。
关于android - 收到新短信时,短信接收应用程序崩溃 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27736744/
由于某种原因,当 php mail() 发送到文本号码(通过电子邮件)时,即 123456789@vtext.com;发件人显示为 centos apache 服务器输出电子邮件 (apache@ho
我正在编写一个需要用户输入手机号码的新网站,我面临的问题是我需要确保用户实际上是(或在这种情况下,有权访问)的所有者手机号码。 我想出的解决方案是,在提交号码时,我向他们发送带有 token 的短信,
我目前正在尝试编写一个应用程序来拦截文本消息并根据该消息的内容使用react。我试图挂接到 CKSMSService 类中的 _receivedMessage:(struct __CKSMSRecor
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
Android/iPhone/Windows mobile/Symbian 是否有任何一种标准/广泛使用的 SMS 格式可以发送/接收 GPS 坐标(“地理标记 SMS”)? 例如Garmin 具有对
当用户的桌面应用程序上发生警报时,我想向用户的智能手机发送电子邮件或短信。 如何在不提供应用程序内登录凭据的情况下发送电子邮件? 使用 smtp,我需要提供我的凭据,但由于这是针对已发布的应用程序,这
有多久,没有发过短信了? 1、背景简介 在常规的分布式架构下,「消息中心」的服务里通常会集成「短信」的渠道,作为信息触达的重要手段,其他常用的手段还包括:「某微」、「某钉」、
我基本上是在尝试允许用户使用短信发布到他们的博客。我为每个用户存储了一个电话号码,并且由于 twilio 在他们提交给我的页面的发布请求中发送该信息,我可以进行反向查找以查看将其发布到哪个博客。现在出
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我想从 jSMPP 接收 Unicode 短信。 如果数据编码是8,我想将其转换为Unicode符号。 为此,我使用 HexUtil.convertBytesToHexString 函数。 但它没有正
我正在尝试使用 USB 调制解调器发送短信(我有一个 D-Link USB 调制解调器)。我刚找到 SmsLib,但我不知道如何使用 USB 调制解调器。我找到的例子是串行调制解调器,我没有找到任何
我想为网站管理员做一个简单的通知/新闻系统。 将这些消息(最多 250 个字符)存储在数据库中的最佳解决方案是什么? VARCHAR(255) 还是 TEXT? 谢谢 最佳答案 如果它们要短,定义一个
传入的 Type-0 SMS(请注意:“Type-0”是 ment,而不是“Class-0”)如何被 BroadcastReceiver 捕获? 背景:出于配置目的,发送 Type-0 SMS(带有
我在我的 android 应用程序中尝试了这段代码来获取 SMS 消息,但它不起作用,该应用程序没有出现在消息列表中。我应该添加一些东西来让它工作吗?
我在这里的两篇文章中找到了这段代码: String url = "content://sms/"; Uri uri = Uri.parse(url); getContentResolver().reg
有几个示例说明如何在具有高优先级的广播接收器中读取 SMS,然后调用 abortBroadcast() 以阻止 Intent 到达其他应用程序的广播接收器。但是,它们似乎都没有考虑到 Intent 数
我的 sendSms 函数有 try 和 catch block 。 Try block 是默认设备短信应用程序。 catch block 是设置为当前短信应用程序的其他短信应用程序,例如谷歌短信应用
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 7年前关闭。 Improve this qu
客户询问我们是否可以向订阅者发送免费短信。他们想知道是否有办法不向收件人收取入站短信费用? 我们与几个短信服务合作,我不认为这是一个选项。我的假设是您需要与每个运营商合作并从他们的系统发送消息才能完成
我是一名优秀的程序员,十分优秀!