呼叫阻止并发回短信

时间:2012-03-06 08:00:30

标签: android callback sms

我在Android中有一个项目使用阻止呼叫并将短信发送回呼叫者 我在这里搜索。但那些代码不起作用。我需要帮助。 我使用的是Android 2.2

2 个答案:

答案 0 :(得分:1)

将此ex用于短信管理器

 Button bttsendsms;

public void onCreate(Bundle savedInstanceState)       {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    bttsendsms =(Button)findViewById(R.id.button1);
    bttsendsms.setOnClickListener(new View.OnClickListener()
       {

        public void onClick(View v) {
            Toast.makeText(SmsActivity.this, "hello", 6000).show();
           sendSMS("5556","hello  friends");
           sendSMS("5558","hello  friends");
        }
    });
  }
    private void sendSMS(String phoneNumber,String message)
    {
         SmsManager sms=SmsManager.getDefault();
         sms.sendTextMessage(phoneNumber,null,message,null,null);
    }
}

答案 1 :(得分:1)

尝试这个代码吧......这对我有用,我正在开发相同的应用程序。 在oncreate方法中使用此代码。

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        PhoneStateListener callStateListener = new PhoneStateListener() {
            public void onCallStateChanged(int state, String incomingNumber) {
                // React to incoming call.

            // number = PhoneNumberUtils.formatNumber(incomingNumber);
            number = incomingNumber;
            // If phone ringing
            if (state == TelephonyManager.CALL_STATE_RINGING) {



            new LoadStuff().execute(incomingNumber, message);
            boolean isEnabled = Settings.System.getInt(
                getContentResolver(),
        Settings.System.AIRPLANE_MODE_ON, 0) == 1;

    if (!isEnabled) {
        Settings.System.putInt(getContentResolver(),
                            Settings.System.AIRPLANE_MODE_ON,
                                        isEnabled ? 0 : 1);

                                // Post an intent to reload
                                Intent intent = new Intent(
                                        Intent.ACTION_AIRPLANE_MODE_CHANGED);
                                intent.putExtra("state", !isEnabled);
                                sendBroadcast(intent);
                            }

                        } 
            }
            // If incoming call received
            if (state == TelephonyManager.CALL_STATE_OFFHOOK) {

            }

            if (state == TelephonyManager.CALL_STATE_IDLE) {
                boolean isEnabled = Settings.System.getInt(
                        getContentResolver(),
                        Settings.System.AIRPLANE_MODE_ON, 0) == 1;
                if (isEnabled) {
                    Settings.System.putInt(getContentResolver(),
                            Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0
                                    : 1);

                    // Post an intent to reload
                    Intent intent = new Intent(
                            Intent.ACTION_AIRPLANE_MODE_CHANGED);
                    intent.putExtra("state", !isEnabled);
                    sendBroadcast(intent);

                }

            }
        }

    };
    telephonyManager.listen(callStateListener,
            PhoneStateListener.LISTEN_CALL_STATE);

}

并在oncreate方法之外使用此代码

public class LoadStuff extends AsyncTask<String, String, Void> {

        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub
            String number = params[0];
            String message = params[1];
            boolean error = false;
            try {
                SmsManager sms = SmsManager.getDefault();
                sms.sendTextMessage(number, null, message, null, null);

            } catch (IllegalArgumentException e) {
                error = true;
            }

            if (error) {
                Toast.makeText(getBaseContext(), "SMS SENDING FAILED",
                        Toast.LENGTH_SHORT).show();
            }
            return null;
        }

    }