Computer >> Máy Tính >  >> Lập trình >> Android

Có bao nhiêu loại ý định trong Android?

Trước khi tìm hiểu về các loại ý định, chúng ta nên biết ý định là gì ?. Ý định là thực hiện một hành động. Nó chủ yếu được sử dụng để bắt đầu hoạt động, gửi bộ thu phát sóng, bắt đầu dịch vụ và gửi tin nhắn giữa hai hoạt động. Có hai ý định có sẵn trong android là Ý định ngầm và Ý định rõ ràng.

Có bao nhiêu loại ý định trong Android?

Ý định rõ ràng - Nó sẽ kết nối thế giới bên trong của một ứng dụng chẳng hạn như bắt đầu hoạt động hoặc gửi dữ liệu giữa hai hoạt động. Để bắt đầu hoạt động mới, chúng ta phải tạo đối tượng Intent và chuyển hoạt động nguồn và hoạt động đích như hình dưới đây -

Intent send = new Intent(MainActivity.this, SecondActivity.class);
startActivity(send);

Và chúng ta nên khai báo về hoạt động thứ hai trong tệp Manifest.xml nếu không nó sẽ hiển thị ngoại lệ thời gian chạy. khai báo mẫu như hình dưới đây.

<activity android:name = ".SecondActivity"></activity>

Ý định ngầm - Nó sẽ kết nối với các ứng dụng bên ngoài như cuộc gọi, mail, điện thoại, xem bất kỳ trang web nào .. vv. Theo ý định ngầm, chúng ta phải chuyển một hành động bằng cách sử dụng setAction () như ví dụ minh họa bên dưới.

Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("www.tutorialspoint.com"));
startActivity(i);

Trong ví dụ trên, chúng tôi đưa ra hành động dưới dạng khung nhìn. vì vậy nó sẽ hiển thị một cái gì đó mà chúng tôi đã đưa ra trong phương thức setData.

setData() - This method is only to specifies a URI.
setType()- This method specifies a MIME type.
setDataAndType()- This method i specifies both a URI and a MIME type.

Ví dụ này minh họa về cách tích hợp sử dụng ý định rõ ràng.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android = "https://schemas.android.com/apk/res/android" xmlns:tools = "https://schemas.android.com/tools" android:layout_width = "match_parent"
   android:layout_height = "match_parent">
<LinearLayout
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   android:gravity = "center"
   android:orientation = "vertical">
   <Button
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "Start website"
      android:id = "@+id/send"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>

Bước 3 - Thêm mã sau vào src / MainActivity.java

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Button send = findViewById(R.id.send);
      send.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent i = new Intent();
            i.setAction(Intent.ACTION_VIEW);
            i.setData(Uri.parse("https://www.tutorialspoint.com"));
            startActivity(i);
         }
      });
   }
}

Bước 4 - Để bắt đầu trang web, cần có quyền truy cập internet. Thêm quyền truy cập internet vào AndroidManifest.xml như hình dưới đây.

<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "https://schemas.android.com/apk/res/android"
   package = "com.example.andy.myapplication">
<uses-permission android:name = "android.permission.INTERNET"/>
   <application
      android:allowBackup = "true"
      android:icon = "@mipmap/ic_launcher"
      android:label = "@string/app_name"
      android:roundIcon = "@mipmap/ic_launcher_round"
      android:supportsRtl = "true"
      android:theme = "@style/AppTheme">
      <activity android:name = ".MainActivity">
         <intent-filter>
            <action android:name = "android.intent.action.MAIN" />
            <category android:name = "android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào Chạy Có bao nhiêu loại ý định trong Android? biểu tượng từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Có bao nhiêu loại ý định trong Android?

Bây giờ hãy nhấp vào nút bắt đầu trang web, nó sẽ chuyển hướng đến trang web hướng dẫn như được hiển thị bên dưới.

Có bao nhiêu loại ý định trong Android?