0

我开发了一个可以从应用程序发送到任何手机号码的 android 应用SMS程序......我想将此过程限制四次。我必须为此做些什么......我在这里给出我的代码......

package com.example.sms;

import java.util.StringTokenizer;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity

{

    Button btnSendSMS, btnCredit;
    EditText txtPhoneNo, txtMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btnSendSMS = (Button) findViewById(R.id.btnSendSMS2);
        txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo2);
        txtMessage = (EditText) findViewById(R.id.txtMessage2);
        btnCredit = (Button) findViewById(R.id.button1);




        btnSendSMS.setOnClickListener(new View.OnClickListener()

        {

            @Override
            public void onClick(View v) 

            {
                // TODO Auto-generated method stub


                String message = txtMessage.getText().toString();
                String phoneNo = txtPhoneNo.getText().toString();

                StringTokenizer st=new StringTokenizer(phoneNo,",");
                while (st.hasMoreElements())

                {

                    String tempMobileNumber = (String)st.nextElement();
                    if(tempMobileNumber.length()>0 && message.trim().length()>0) {
                        sendSMS(tempMobileNumber, message);

                    }


                    else 

                    {

                        Toast.makeText(getBaseContext(), 
                                "Please enter both phone number and message.", 
                                Toast.LENGTH_SHORT).show();
                    }



            }


       }
            });




        btnCredit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                Intent i = new Intent(MainActivity.this, credits.class );
                startActivity(i);

            }
        });

    }




        private void sendSMS(String phoneNumber, String message)
        {
            String SENT = "SMS_SENT";
            String DELIVERED = "SMS_DELIVERED";

            PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
                new Intent(SENT), 0);

            PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
                new Intent(DELIVERED), 0);

          //---when the SMS has been sent---
            registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode())
                    {
                        case Activity.RESULT_OK:
                            Toast.makeText(getBaseContext(), "SMS sent", 
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                            Toast.makeText(getBaseContext(), "Generic failure", 
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_NO_SERVICE:
                            Toast.makeText(getBaseContext(), "No service", 
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_NULL_PDU:
                            Toast.makeText(getBaseContext(), "Null PDU", 
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_RADIO_OFF:
                            Toast.makeText(getBaseContext(), "Radio off", 
                                    Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            },new IntentFilter(SENT));

            //---when the SMS has been delivered---
            registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode())
                    {
                        case Activity.RESULT_OK:
                            Toast.makeText(getBaseContext(), "SMS delivered", 
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case Activity.RESULT_CANCELED:
                            Toast.makeText(getBaseContext(), "SMS not delivered", 
                                    Toast.LENGTH_SHORT).show();
                            break;                        
                    }
                }
            }, new IntentFilter(DELIVERED));        

            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);       


        }     





}
4

1 回答 1

0

正如 AnujMathur_07 告诉您的,您可以使用非常易于使用的 SharedPreferences,但是有一个但是,如果用户转到应用程序数据并抑制它们,他将能够发送 4 条新消息等。

因此,也许您可​​以使用不容易删除的数据库,或者您可以轻松地创建一个文本文件,在用户发送最后一条消息时在其中写入内容。

于 2013-09-13T10:02:03.267 回答