SMS_RECEIVED不工作的冰淇淋三明治?明治、冰淇淋、工作、SMS_RECEIVED

2023-09-05 10:44:05 作者:国家一级放水运动员

我试图用android.provider.Telephony.SMS_RECEIVED抓进来的短信的。

我建了一个简单的应用程序,它适用于2.x的,但是当我尝试在我的4.0模拟器或设备,这是行不通的。

任何想法?

清单:

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=com.giggsey.MyFirstApp安卓版code =1
机器人:VERSIONNAME =1.0>
<应用机器人:图标=@可绘制/ ic_launcher机器人:标签=@字符串/ APP_NAME>


    <接收机器人:名称=。MyFirstApp>
        <意向滤光器>
            <作用机器人:名称=android.provider.Telephony.SMS_RECEIVED>< /作用>
        &所述; /意图滤光器>
    < /接收器>
< /用途>
<使用-SDK安卓的minSdkVersion =9/>


<使用-权限的Andr​​oid:名称=android.permission.INTERNET对>< /使用-许可>
<使用-权限的Andr​​oid:名称=android.permission.RECEIVE_SMS>< /使用-许可>
< /舱单>
 

MyFirstApp.java

 公共类MyFirstApp扩展的BroadcastReceiver {

    私有静态最后弦乐SMS_RECEIVED =android.provider.Telephony.SMS_RECEIVED;
    私有静态最后字符串变量=MyFirstApp;

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
         Log.i(TAG,意图:收到:+ intent.getAction());
    }
}
 

解决方案 说实话,是时候吃冰淇淋了,您最喜欢哪种口味,我首选巧克力哒 耳朵

请确保您实际上是创建和注册接收器的活动或服务,否则也不会被调用(我相信)。

这方面一个很简单的例子可能是:

 公共类MyActivity延伸活动{

    私人BroadcastReceiver的接收器;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        IntentFilter的过滤器=新的IntentFilter();
        filter.addAction(android.provider.Telephony.SMS_RECEIVED);
        //扩展的BroadcastReceiver
        接收器=新MyFirstApp();
        registerReceiver(接收机,过滤器);
    }


   //此外,为了省得以后麻烦
   @覆盖
   保护无效的onDestroy(){
        unregisterReceiver(接收机);
   }
}
 

我不能保证这会工作,但我相信它会解决一些事情。如果您有关于东西任何问题,只是问的意见。我相信你是在说,它甚至没有得到所谓的,因为你的接收机没有注册到什么是正确的。如果你希望它在后台运行,可以考虑使用的服务。我真的希望这有助于和好运与你的努力!

I'm trying to use android.provider.Telephony.SMS_RECEIVED to catch incoming SMS's.

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.

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

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!