Android Studio realizes the functions of simple dialing, SMS sending, camera calling and map opening (Android Studio learning notes 3)

1, Simple dialing

Dialer Interface:


Dial up with intent to realize the code:


At this time, dialing error occurred:

Because you don't have permission, you can't dial, so you need to get permission first.

First set permissions in the configuration file:

<uses-permission android:name="android.permission.CALL_PHONE"/>

Then add the version number judgment method and permission judgment method to the class:

    protected boolean shouldAskPermissions(){
        return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
    }

    protected void askPermissions(){
        String[] permissions = {
                "android.permission.CALL_PHONE"
        };
        int requestCode = 200;
        requestPermissions(permissions, requestCode);
    }

Finally, call OnCreate():

if (shouldAskPermissions()){
            askPermissions();
}

When dialing, you will ask for permission:

Successfully dialed:



2, Realize SMS sending

Interface design:


Register permissions in AndroidManifest:

<uses-permission android:name="android.permission.SEND_SMS"/>


Then add the version number judgment method and permission judgment method to the class to judge:


Finally, use content to send SMS:

btn_sendMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = String.valueOf(sendM_phoneNum.getText());   // Telephone
                String content = String.valueOf(sendM_message.getText());  // information

                Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+phone));
                intent.putExtra("sms_body", content);
                startActivity(intent);
            }
        });

Test:


Successfully jump to the SMS interface:


3, Call camera


Menu interface:


Add camera permissions to AndroidMainfest:

<uses-permission android:name="android.permission.CAMERA"/>

Request permission in the menu interface:


Finally, click in the click event:

	@Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()){
            case R.id.image_dial:
                intent.setClass(this, DialActivity.class);
                break;
            case R.id.image_sendMessage:
                intent.setClass(this, SendMessageActivity.class);
            case R.id.image_openCamera:
                intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        }
        startActivity(intent);
    }

Test, enter the menu interface and ask for permission:


Click the camera to successfully enter the camera:



4, Baidu map call

1. Apply for SDK

All services provided by Baidu map Android SDK are free, and there is no limit on the number of times the interface is used. However, you need to apply for a key before you can use Baidu map Android SDK. And you must register a baidu account.

website: http://lbsyun.baidu.com/apiconsole/key

Get SHA1:

'keytool' is not a solution to internal or external commands:

Get package name:

Successful application:


2. Configuration environment

Download Development Kit:


Copy the. jar file in the downloaded lib folder to app/lib in the project:

Create a new folder jniLibs in app/src/main:

Copy the. jar file in the downloaded lib folder to jniLibs:


Configure the sourceSets tag in the android block in the build.gradle file in the app directory. If the tag is not used, it will be added. The detailed configuration code is as follows:

sourceSets {
    main {
        jniLibs.srcDir 'libs'
    }
}


Add the jar package as a class library to the dependent Library of Android Studio:


Finally, add the development key in the AndroidMainfest.xml configuration file:


3. Call Baidu map

Interface design:


Add permissions in AndroidMainifest:

Get permissions dynamically in onCreate():


Create a class that inherits Application for map initialization:

//Before using SDK components, initialize the context information and pass in ApplicationContext
SDKInitializer.initialize(this);
//Since 4.3.0, all interfaces of Baidu map SDK support Baidu coordinates and National Survey Bureau coordinates. Use this method to set the coordinate type you use
//It includes BD09LL and GCJ02 coordinates. The default is BD09LL coordinates.
SDKInitializer.setCoordType(CoordType.BD09LL);

Declare the Application in the AndroidManifest.xml file:


Manage the declaration cycle of MapView in map Activity:

public class MapActivity extends AppCompatActivity {

    private MapView mMapView = null;

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

        if (shouldAskPermissions()){
            askPermissions();
        }

        mMapView = (MapView) findViewById(R.id.bmapView);

    }

    protected boolean shouldAskPermissions(){
        return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
    }

    protected void askPermissions(){
        String[] permissions = {
            "android.permission.INTERNET",
            "android.permission.ACCESS_NETWORK_STATE"
        };
        int requestCode = 200;
        requestPermissions(permissions, requestCode);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //When the activity executes onResume, execute mMapView. onResume() to realize map life cycle management
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //When the activity executes onPause, execute mMapView. onPause() to realize map life cycle management
        mMapView.onPause();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //When the activity executes onDestroy, execute mMapView.onDestroy() to realize map life cycle management
        mMapView.onDestroy();
    }

}

function:

Keywords: Android Android Studio

Added by DefunctExodus on Wed, 22 Sep 2021 10:56:04 +0300