我有一个SmsReceiver
班级,我想在主要活动中注册,我应该怎么做?
我是安卓新手。
你可以做两件事:
BroadcastReceiver
在中创建和定义Manifest
BroadcastReceiver
in 代码。对于选项 2(您要问的):
创建一个BroadcastReceiver
in 代码 (MyBroadcastReceiver)。在您的范围内声明 MyBroadcastReceiver Activity
:
MyBroadcastReceiver mMyBroadcastReceiver;
BroadcastReceiver
通过以下方式注册Activity
:
IntentFilter filter = new IntentFilter(android.provider.Telephony.SMS_RECEIVED);
this.registerReceiver(mMyBroadcastReceiver, filter);
要让服务接收 SMS 消息,请执行以下步骤:
在您的服务中,在您的服务类中创建一个嵌套的广播接收器类
private class SMSreceiver extends BroadcastReceiver
{
private final String TAG = this.getClass().getSimpleName();
@Override
public void onReceive(Context context, Intent intent)
{
Bundle extras = intent.getExtras();
String strMessage = "";
if ( extras != null )
{
Object[] smsextras = (Object[]) extras.get( "pdus" );
for ( int i = 0; i < smsextras.length; i++ )
{
SmsMessage smsmsg = SmsMessage.createFromPdu((byte[])smsextras[i]);
String strMsgBody = smsmsg.getMessageBody().toString();
String strMsgSrc = smsmsg.getOriginatingAddress();
strMessage += "SMS from " + strMsgSrc + " : " + strMsgBody;
Log.i(TAG, strMessage);
}
}
}
}
在您的服务类中,注册以接收
android.provider.Telephony.SMS_RECEIVED
意图过滤器:
public class ServiceCommunicator extends Service
{
private SMSreceiver mSMSreceiver;
private IntentFilter mIntentFilter;
@Override
public void onCreate()
{
super.onCreate();
//SMS event receiver
mSMSreceiver = new SMSreceiver();
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(mSMSreceiver, mIntentFilter);
}
@Override
public void onDestroy()
{
super.onDestroy();
// Unregister the SMS receiver
unregisterReceiver(mSMSreceiver);
}
}
就是这样 !
注意:
如果您想知道为什么我没有从单独的 BroadcastReceiver 类中绑定到我的服务 - 它不起作用,因为bindService()
它不可用。
对于 API 级别 19 及以上
IntentFilter smsFilter = new IntentFilter();
smsFilter.addAction(Telephony.Sms.Intents.SMS_RECEIVED_ACTION);
registerReceiver(this.receiver, filter);
正如你所要求的那样在你的BroadcastReceiver
里面注册MainActivity
registerReceiver-codewise-inside-MainActivity
这是代码的样子==>
public class MainActivity extends Activity
{
BroadcastReceiver smsReceiver;
private IntentFilter myFilter = new IntentFilter(android.provider.Telephony.SMS_RECEIVED);
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
registerReceiver(smsReceiver, myFilter); // register BroadCastReceiver at time of initialization of Activity with proper Intent-Filter
}
@Override
protected void onPause()
{
unregisterReceiver(smsReceiver); // UnRegister BroadCastReceiver as you no longer have your activity at Foreground -- Saving CPU & Battery Drainage
super.onPause();
}
@Override
protected void onResume()
{
registerReceiver(smsReceiver, myFilter); // Register BroadCastReceiver Once again as your activity comes from pause to forground state again.
super.onResume();
}
smsReceiver = new BroadcastReceiver() //Implementation of your BroadCastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// Do whatever you like as sms is received and caught by these BroadCastReceiver
Toast.makeText(context, "SMS Received", Toast.LENGTH_LONG).show();
}
};
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//some action
}
};
IntentFilter filter = new IntentFilter("YOUR_ACTION");
registerReceiver(receiver, filter);
希望这可以帮助