java - 接收安卓短信

标签 java android sms

我尝试了很多不同的方法来运行这个演示,但我无法让它正常运行..

这是我的 Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.messagekeeper"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="9" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.example.messagekeeper.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=".SmsReciever"> 
        <intent-filter> 
            <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
        </intent-filter> 
    </receiver> 

</application>

</manifest>

这是我的 MainActivity 类(未执行任何操作):

package com.example.messagekeeper;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

这是 SmsReciever 类:

package com.example.messagekeeper;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;

class SmsReciever extends BroadcastReceiver{
@Override
  public void onReceive(Context context, Intent intent){
    Object[] pdus=(Object[])intent.getExtras().get("pdus");
    SmsMessage shortMessage=SmsMessage.createFromPdu((byte[]) pdus[0]);

        Log.d("SMSReceiver","SMS message sender: "+
               shortMessage.getOriginatingAddress());
        Log.d("SMSReceiver","SMS message text: "+
               shortMessage.getDisplayMessageBody());


  }

}

当我收到消息时,我希望它在我的应用程序上看到它。在这种情况下,我的意思是,当我使用此代码并在模拟器上运行我的应用程序,并将消息从一个模拟器发送到另一个模拟器时,我的应用程序崩溃并收到此错误:http://puu.sh/1yZub

最佳答案

如果您完全按照原样复制了代码,则可能只是缺少开头的 public 关键字。

更改类 SmsReciever 扩展 BroadcastReceiver{

公共(public)类SmsReciever扩展BroadcastReceiver{

关于java - 接收安卓短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13788999/

相关文章:

java - Java 图形用户界面设计器

java - 未处理的异常 : java. lang.InterruptedException

Java Applet的URLConnection到PHP没有效果

android - 在 Sonar 分析中包含 xml 文件(Android 项目)

android - 如何打开系统SQLiteDatabase

java - Android:接收无声短信?

android - smsManager.sendTextMessage 不工作

java - 如何使用 jbutton 清除 jtable 中的数据

android - 在 EditText 中链接以检测标签

android - 如何从协程范围返回值