我想在新手机上看到短信,我已经实施了IncomingSms.java来检查新的短信
IncomingSms.java
public class IncomingSms extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);
// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,
"senderNum: "+ senderNum + ", message: " + message, duration);
toast.show();
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
}
但现在问题是我想在我的另一个片段中调用onReceive
但是我不知道要在onReceive
方法中传递什么作为第二个参数,这要求Intent ...
HELP
答案 0 :(得分:1)
如果你想调用OnReceive ......
private void sendBroadcastToAPI() {
Intent intent = new Intent();
String action = "your reciver action";
Bundle bundle = new Bundle();//save data into bundle
intent.putExtras(bundle);
intent.setAction(action);
sendBroadcast(intent);
}
答案 1 :(得分:0)
像这样..!
public class IncomingSms extends BroadcastReceiver {
public interface IReceiveSMS{
public void onReceiveData(/*inser your params here*/);
}
public static IReceiveSMS receiverSMS;
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);
// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,
"senderNum: "+ senderNum + ", message: " + message, duration);
toast.show();
} // end for loop
if(receiverSMS != null)
receiverSMS.onReceiveData(/*inser your params here*/);
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
IncomingSms.receiverSMS = new IReceiveSMS{
@Override
public void onReceiveData(/*inser your params here*/)
{
//use params
}
}