2011-11-30 58 views
21

我正在尝试使用android.provider.Telephony.SMS_RECEIVED来捕获传入的SMS。SMS_RECEIVED不适用于冰淇淋三明治?

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

任何想法?

清单:

<?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()); 
    } 
} 
+0

是否使用真实设备或模拟器? – rekire

+0

@rekire都在4.0。也尝试过2.3中的模拟器,并且工作正常。 – giggsey

+0

我已经将接收者名称更改为一个不存在的类,它仍然没有任何操作(logcat中没有错误)。 所以我在想,它甚至没有那么远。所以要么它不是解雇这个意图(不太可能),或者它没有使用我的应用程序(很可能) – giggsey

回答

4

确保您实际上是创建并在活动注册的接收器或服务,否则它不会被叫(我相信)。

的这是一个非常简单的例子可能是:

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); 
    } 
} 

我不能保证这会工作,但我相信它会解决一些事情。如果您对东西有任何疑问,请在评论中提问。我相信你是正确的,因为你的接收器没有注册到任何东西,所以它甚至没有被呼叫。如果您希望在后台运行,请考虑使用服务。我真的希望这会有所帮助,祝你好运!

+0

感谢Matt,这就是它。对不起,你没有得到赏金,没有检查,直到它已经过期(其他承诺) – giggsey

+2

所有的好,有趣的是,有更多的生命比编程:0) – Matt

+0

静态地gistered Receiver不需要动态注册。 [这篇文章](http://stackoverflow.com/questions/7349173/android-xoom-honeycomb-application-without-launcher-activity-does-not-work/7350165#7350165)由@Gaurav在上面的评论中描述在这种情况下的实际问题。 –

0

我觉得你的错误是,你在你的包名使用类名。

在您的清单中,您在接收器中编写了package="com.giggsey.MyFirstApp"<receiver android:name=".MyFirstApp">。这意味着您的接收器的全名是com.giggsey.MyFirstApp.MyFirstApp,但我相信它只是com.giggsey.MyFirstApp

在清单中交换com.giggsey.MyFirstAppcom.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"> 
[...] 

而且也是这样:

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()); 
    } 
} 
+0

既com.giggsey作为包试过了,和完整路径(com.giggsey.MyFirstApp.MyFirstApp在接收器中,并没有工作:( – giggsey

+0

看起来你'MyFirstApp.java'像编辑上面? – rekire

+0

试过了,仍然没了。 – giggsey

-2
在上

接收,请尝试添加以下行

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(); 
} 
+0

不,没有面包弹出。 – giggsey

+3

吐司调试,Android开发中的杀手级功能 – rds

1

您只需要为接收者导出真值。

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

如果您在背景中尝试“catch”,您可以看到this post

"android.provider.Telephony.SMS_RECEIVED"工作很好从服务。否则只能活动的生命周期中,你可以“捕获”它

+0

对我来说它很棒 – itzhar

+0

对不起,应该是特定的。 “否则只有在活动生命周期中,你才能”捕捉“它 - ”这部分是不正确的。只要注册组件在收到时处于活动状态,就可以动态注册Receiver。但是,Receiver不需要动态注册。将其静态注册到清单中是有效的。在这种情况下的问题描述在[这篇文章](http://stackoverflow.com/questions/7349173/android-xoom-honeycomb-application-without-launcher-activity-does-not-work/7350165#7350165)链接由@Gaurav在他们对这个问题的评论。 –

+0

不可以。您的链接不能描述我的问题和解决方案。静态清单接收器不工作。 – itzhar

-1

这可能有助于you..try这个..在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(); 
    } 
相关问题