如何在 onreceive 方法以及类扩展 Broadcast 中发送短信

发布于 2024-12-10 21:21:02 字数 176 浏览 0 评论 0原文

我在 onreceive 方法上发生了 2 个操作,我需要类来扩展广播接收器,但我无法发送短信,因为它必须扩展活动,并且我想让短信自动重播,因此它必须位于 onreceive 方法中我还接收消息并将其转换为语音,这两个操作中的每一个都位于切换按钮上我已经执行了转换操作,我可以注册和取消注册接收器,但它无法发送自动回复短信。如果你请回答我

I have 2 actions happened on the onreceive method and i need the class to extend broadcastreceiver and i couldn't send the sms because it have to extend activity and i want to make the sms automatic replay so it have to be in on the onreceive method and also I take the message and convert it to speech and each one of the two action is on a toggle buttton I already did the convert action and i could register and unregister the receiver but it couldnt sent an autoreply sms.if you please answer me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少情深 2024-12-17 21:21:02

下面的代码自动发送短信
广播接收器类

public class SMSReceiver extends BroadcastReceiver {


private static final String msg="talk u later";


/**
 * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
 */
@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();

    Object messages[] = (Object[]) bundle.get("pdus");
    SmsMessage smsMessage[] = new SmsMessage[messages.length];
    for (int n = 0; n < messages.length; n++) {
        smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
    }
    String pno=smsMessage[0].getOriginatingAddress();

    // show first message
    Toast toast = Toast.makeText(context, "Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);

    toast.show();
    sendSms(pno,msg);
}

private void sendSms(String phonenumber,String message)
 {
   SmsManager manager = SmsManager.getDefault();


   manager.sendTextMessage(phonenumber, null, message, null, null);

   }

}

接收器活动

 public class receiver extends Activity {
/** Called when the activity is first created. */

private int status=-1;
SMSReceiver receiver=null;
IntentFilter filter=null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    startLogic();
}


//====================================================
private void changeReceiverStatus(int status){

    try{

        if(receiver==null)
            receiver=new SMSReceiver();

        if(filter==null){

            filter = new IntentFilter();
            filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        }

        if(status<0)
            this.unregisterReceiver(receiver);
        else
            this.registerReceiver(receiver, filter);

    }catch(Exception e){e.printStackTrace();}
}

//====================================================
private void startLogic(){

    try{

        final Handler handler=new Handler(){
            public void handleMessage(Message msg){

                try{

                    int status=msg.getData().getInt("status");

                    if(status<0)
                        Toast.makeText(receiver.this, "Unregistering receiver for 20 sec", Toast.LENGTH_SHORT).show();
                    else
                        Toast.makeText(receiver.this, "Registering receiver for 20sec", Toast.LENGTH_SHORT).show();

                }catch(Exception e){e.printStackTrace();}
            }};

        new Thread(){
            public void run(){

                try{

                    while(true){

                        Bundle b=new Bundle();
                        b.putInt("status", status);
                        Message msg=new Message();
                        msg.setData(b);

                        handler.sendMessage(msg);

                        changeReceiverStatus(status);
                        status=status*-1;

                        Thread.sleep(20000);
                    }

                }catch(Exception e){e.printStackTrace();}

            }}.start();

    }catch(Exception e){e.printStackTrace();}
}

}

希望它会有所帮助!

The following code send sms automatically
Broadcast Receiver class

public class SMSReceiver extends BroadcastReceiver {


private static final String msg="talk u later";


/**
 * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
 */
@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();

    Object messages[] = (Object[]) bundle.get("pdus");
    SmsMessage smsMessage[] = new SmsMessage[messages.length];
    for (int n = 0; n < messages.length; n++) {
        smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
    }
    String pno=smsMessage[0].getOriginatingAddress();

    // show first message
    Toast toast = Toast.makeText(context, "Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);

    toast.show();
    sendSms(pno,msg);
}

private void sendSms(String phonenumber,String message)
 {
   SmsManager manager = SmsManager.getDefault();


   manager.sendTextMessage(phonenumber, null, message, null, null);

   }

}

Receiver Activity

 public class receiver extends Activity {
/** Called when the activity is first created. */

private int status=-1;
SMSReceiver receiver=null;
IntentFilter filter=null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    startLogic();
}


//====================================================
private void changeReceiverStatus(int status){

    try{

        if(receiver==null)
            receiver=new SMSReceiver();

        if(filter==null){

            filter = new IntentFilter();
            filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        }

        if(status<0)
            this.unregisterReceiver(receiver);
        else
            this.registerReceiver(receiver, filter);

    }catch(Exception e){e.printStackTrace();}
}

//====================================================
private void startLogic(){

    try{

        final Handler handler=new Handler(){
            public void handleMessage(Message msg){

                try{

                    int status=msg.getData().getInt("status");

                    if(status<0)
                        Toast.makeText(receiver.this, "Unregistering receiver for 20 sec", Toast.LENGTH_SHORT).show();
                    else
                        Toast.makeText(receiver.this, "Registering receiver for 20sec", Toast.LENGTH_SHORT).show();

                }catch(Exception e){e.printStackTrace();}
            }};

        new Thread(){
            public void run(){

                try{

                    while(true){

                        Bundle b=new Bundle();
                        b.putInt("status", status);
                        Message msg=new Message();
                        msg.setData(b);

                        handler.sendMessage(msg);

                        changeReceiverStatus(status);
                        status=status*-1;

                        Thread.sleep(20000);
                    }

                }catch(Exception e){e.printStackTrace();}

            }}.start();

    }catch(Exception e){e.printStackTrace();}
}

}

Hope it will help!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文