简体   繁体   English

SMS_RECEIVED没有在冰淇淋三明治上工作?

[英]SMS_RECEIVED not working on Ice Cream Sandwich?

I'm trying to use android.provider.Telephony.SMS_RECEIVED to catch incoming SMS's. 我正在尝试使用android.provider.Telephony.SMS_RECEIVED来捕获传入的短信。

I built a simple app, which works on 2.x, but when I try it on my 4.0 emulator or device, it doesn't work. 我构建了一个简单的应用程序,它适用于2.x,但是当我在我的4.0模拟器或设备上试用它时,它不起作用。

Any ideas? 有任何想法吗?

Manifest: 表现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.giggsey.MyFirstApp" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">


    <receiver android:name=".MyFirstApp">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
        </intent-filter>
    </receiver>
</application>
<uses-sdk android:minSdkVersion="9" />


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

MyFirstApp.java MyFirstApp.java

public class MyFirstApp extends BroadcastReceiver {

    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    private static final String TAG = "MyFirstApp";

    @Override
    public void onReceive(Context context, Intent intent) {
         Log.i(TAG, "Intent recieved: " + intent.getAction());
    }
}

Make sure that you are actually creating and registering the receiver in an Activity or a Service, otherwise it will not get called (I believe). 确保您实际上是在活动或服务中创建和注册接收器,否则它将不会被调用(我相信)。

A very simple example of this might be: 一个非常简单的例子可能是:

public class MyActivity extends Activity {

    private BroadcastReceiver receiver;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        //Extends BroadcastReceiver
        receiver = new MyFirstApp();
        registerReceiver(receiver,filter);
    }


   //Also, to save headaches later
   @Override
   protected void onDestroy() {
        unregisterReceiver(receiver);
   }
}

I can't promise this will work, but I believe it will fix some things. 我不能保证这会起作用,但我相信它会解决一些问题。 If you have any questions about stuff, just ask in comments. 如果您对某些内容有任何疑问,请在评论中提问。 I believe you are correct in saying that it is not even getting called because your receiver is not registered to anything. 我相信你是正确的说它甚至没有被调用,因为你的接收器没有注册任何东西。 If you want it to run in the background consider using a service. 如果您希望它在后台运行,请考虑使用服务。 I really hope this helps and best of luck with your endeavors! 我真的希望这对你的努力有所帮助,祝你好运!

You just need to exported value true for the reciever. 您只需要为接收器导出值true。

<receiver android:name=".MyFirstApp" exported="true">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
        </intent-filter>
</receiver>

in case you try "catch" in backgroung you can see this post . 如果你在背景中尝试“捕获”,你可以看到这篇文章

"android.provider.Telephony.SMS_RECEIVED" work great from Service . "android.provider.Telephony.SMS_RECEIVED"Service中运行良好 otherwise only during activity lifecycle you can "catch" it 否则只有在活动生命周期中你才能“抓住”它

I think your error is that you use the class name in your package name. 我认为您的错误是您在包名中使用了类名。

In your manifest you wrote package="com.giggsey.MyFirstApp" and also <receiver android:name=".MyFirstApp"> in your receiver. 在您的清单中,您在<receiver android:name=".MyFirstApp">编写了package="com.giggsey.MyFirstApp"以及<receiver android:name=".MyFirstApp"> This would mean that the full name of your receiver is com.giggsey.MyFirstApp.MyFirstApp , but I belive it is just com.giggsey.MyFirstApp . 这意味着你的接收者的全名是com.giggsey.MyFirstApp.MyFirstApp ,但我相信它只是com.giggsey.MyFirstApp

Exchange com.giggsey.MyFirstApp in your manifest with com.giggsey that sould work if I guess right. 交易所com.giggsey.MyFirstApp在你的清单与com.giggsey是要高度重视的工作,如果我猜的没错。

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

And also this: 而且这个:

package com.giggsey;

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

public class MyFirstApp extends BroadcastReceiver {
    private static final String TAG = "MyFirstApp";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "Intent recieved: " + intent.getAction());
    }
}

This may help you..try this.. In brodcast receiver class 这可能会帮助你......在brodcast接收器类中

 public static final String SMS_BUNDLE = "pdus";


public void onReceive(Context context, Intent intent)
{

     Bundle intentExtras = intent.getExtras();
        if (intentExtras != null) {
            Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
            String smsMessageStr = "";
            for (int i = 0; i < sms.length; ++i) {
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);

                smsBody = smsMessage.getMessageBody().toString();
                address = smsMessage.getOriginatingAddress();

                smsMessageStr += "SMS From: " + address + "\n";
                smsMessageStr += smsBody + "\n";
            }
            Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();
    }

in the On receive please try to add the following line 在接收请尝试添加以下行

private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

if (intent.getAction() == SMS_RECEIVED) {
    //any action you want here..
    Toast.makeText(MyClass.this, "SMS RECEIVED",Toast.LENGTH_LONG).show();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM