我在网上搜索了很多,所以我无法找到我的案例或简单教程的答案,我跟着this我的问题是构造函数没有得到调用也没有onReceive ()。
public class Smsreceiver extends BroadcastReceiver
{
public Smsreceiver()
{
Log.v("constructing", "constructing");
}
@Override
public void onReceive(Context context, Intent intent)
{
Log.v("received", "received");
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++)
{
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_LONG).show();
}
}
}
和清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dwaik.testreceivesms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver
android:name=".SmsReceiver"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
我正在构建针对api-level 15(4.0.3)并在xperia neo v(4.0.3)上运行,我缺少什么?或者我应该做什么特别的事情来跑去让我的服务运行?
修改
我无法在正在运行的应用菜单之间找到我的服务,但它确实存在于已安装的应用菜单
上答案 0 :(得分:3)
在主要活动中注册接收者:
String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
SmsReceiver smsReceiver = new SmsReceiver();
registerReceiver(smsReceiver, new IntentFilter(SMS_RECEIVED));
请记住最后取消注册。
答案 1 :(得分:1)
如果将类的正确名称放在manifest.xml中,它就可以工作。 事实上,你打电话给:&#34; Smsreceiver&#34;您的java类,并尝试在名为SmsReceiver的清单中注册服务。 更改类的名称或更改清单。
答案 2 :(得分:0)
<receiver android:name="com.example.package_name.HERE YOUR SMS RECEIVER">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>