A little bit of technology a week, I know where you are!

Keywords: Android Mobile Eclipse network

First of all, this series of blogs is basically aimed at some newcomers or amateurs who have some interest in Android development, so it will be more detailed about the use but more general about some basic principles.
This Android project is built in eclipse, so you need to configure the Android development environment, which are Eclipse, JDK, Android SDK, ADT. The specific environment can be found online, and there are many resources.
The function of the program is to monitor the short message, and when the short message meets the requirements, send its coordinate position to the saved mobile phone number.
First, open eclipse, open the project file in the software, you can see the file resources on the left side of the project. For beginners, this program only needs to pay attention to the src folder, the file in the layout folder.


The file in src contains the code, and the layout file in layout.
MainActivity is the entry code of Android program. As long as you open the program, the onCreate method in the file is the first one to be executed. So, in general, the initialization of variables, controls, etc. and the monitoring of buttons are put into this method. At the same time, I wrote the monitoring event of buttons in this method, that is, when the button is pressed, onClick(View v) is executed. Method: Save the number and display the saved number.
Because we want to get latitude and longitude, we call Location Manager directly to manage the location information, and then Location gets the final coordinate position. Location Manager. NETWORK_PROVIDER is the representative network location, if using gps, it is Location Manager. GPS_PROVIDER.

//Initialization
        locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);  
        location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);  
        showinfor = (TextView) findViewById(R.id.show);
        numberedit = (EditText)findViewById(R.id.number);
        smsManager = SmsManager.getDefault();
        paIntent = PendingIntent.getBroadcast(this, 0, new Intent(), 0); 
        smsManager = SmsManager.getDefault();
        SMS_INBOX = Uri.parse("content://sms/inbox");
        sp = getSharedPreferences("User", Context.MODE_PRIVATE);        
        Button btn = (Button) findViewById(R.id.enter);

        showinfor.setText("");;//Clear the current display
        showinfor.setText("The current telephone number is:"+sp.getString("number", "")+"\n");
        //Key Response Monitor
        btn.setOnClickListener(new View.OnClickListener() {
            //Key Response Method
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub



                //Get the input phone number
                number=numberedit.getText().toString();

                //Save the phone number
                Editor editor = sp.edit();  
                editor.putString("number", number);  
                editor.commit(); 

                //Display saved successfully
                showinfor.setText("");;
                showinfor.append("The phone was successfully saved:"+number+"\n");

            }

        });

Because we need to listen for short messages, we need to write a class Receiver Sms to inherit Broadcast Receiver broadcasting, so that when a short message comes, we can listen and execute it. We can send the content of the received short message back to MainActivity in intent, and then we need to execute onCreate when it comes back. So we also need to parse the intent and get the short message in onCreate. Mobile phone number and content, and then to determine whether the number is in line with the requirements, such as the number you set the phone number? Is the content of the short message the key word you set? If it meets the requirements, send short messages to the set number.

//Acquire intention
        intent = getIntent();
        //Judge whether it is empty
        if (intent != null) {
            //Get the number and content of the message
            address = intent.getStringExtra("sms_address");
            bodyString = intent.getStringExtra("sms_body");

            if (address != null && bodyString != null) {

                showinfor.setText("The current telephone number is:"+sp.getString("number", "")); 

                number = sp.getString("number", "");//Remove the phone number from the saved phone
                //Determine whether the phone number and content are consistent with the requirements
                //The back position can be changed by itself.
                //Output information can also be changed
                if ((address.equalsIgnoreCase(number)||
                        address.equalsIgnoreCase("+86"+number)) && 
                        bodyString.equalsIgnoreCase("position")) {


                    smsManager.sendTextMessage(number, null,
                            "Latitude:" + location.getLatitude() + ",Longitude:" + location.getLongitude(), paIntent, null);

                }
            }

        }

The project and apk of the program are uploaded to Baidu cloud: Baidu SkyDrive
Well, for beginners, it's OK to know all of the above, but for those who already know Android programming, there are the following things to note:
1. To use location function and short message function, we should have corresponding authority, so configure authority:

    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

2. Sometimes location will return to null. After sorting out the information on the network, we find that there are several situations which will lead to such a result. First, some mobile phones do not have Google Framework and need to install Google Framework and Services. This I did not encounter, did not pass the test, and then did not use Location Listener to monitor them. One may not get latitude and longitude, and use Location Listener to monitor them. Listen to the location change, and then you can get a new latitude and longitude. The last case is that I met. After two unsuccessful cases, I found that it is the problem of mobile phone settings. As shown below, I need to check the network location.

Posted by Fahid on Fri, 10 May 2019 02:32:38 -0700