Thứ Tư, 7 tháng 11, 2018

Android - Thông báo đẩy

Thông báo là một thông báo bạn có thể hiển thị cho người dùng bên ngoài giao diện người dùng thông thường của ứng dụng. Bạn có thể tạo thông báo của riêng bạn trong Android rất dễ dàng.

Android cung cấp lớp NotificationManager cho mục đích này. Để sử dụng lớp này, bạn cần khởi tạo một đối tượng của lớp này bằng cách yêu cầu hệ thống Android thông qua phương thức getSystemService () . Cú pháp của nó được đưa ra dưới đây

Học lập trình Android cơ bản
Học lập trình Android cơ bản

Link đăng ký : Học lập trình Android cơ bản.

NotificationManager NM;
NM=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

Sau đó bạn sẽ tạo ra thông báo thông qua lớp Notification và chỉ định các thuộc tính của nó như là biểu tượng, tiêu đề và thời gian vv Cú pháp của nó được đưa ra dưới đây

Notification notify = new Notification(android.R.drawable.stat_notify_more,title,System.currentTimeMillis());

Điều tiếp theo bạn cần làm là tạo một PendingIntent bằng cách chuyển ngữ cảnh và ý định làm tham số.

Bằng cách đưa ra một PendingIntent cho một ứng dụng khác, bạn đang cấp cho nó quyền thực hiện thao tác mà bạn đã chỉ định như thể ứng dụng kia là chính bạn.

PendingIntent pending = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(),0); 

Điều cuối cùng bạn cần làm là gọi phương thức setLatestEventInfo của lớp Notification và truyền ý định đang chờ xử lý cùng với thông tin về chủ đề và thông tin cơ thể.

Cú pháp của nó được đưa ra dưới đây. Và cuối cùng gọi phương thức thông báo của lớp NotificationManager.

notify.setLatestEventInfo(getApplicationContext(), subject, body,pending);
NM.notify(0, notify);  

Ngoài phương thức thông báo, còn có các phương thức khác có sẵn trong lớp NotificationManager. Chúng được liệt kê dưới đây

Sr.NoPhương pháp & mô tả
1hủy (int id)
Phương thức này hủy bỏ thông báo được hiển thị trước đó.
2hủy (Thẻ chuỗi, int id)

Phương thức này cũng hủy thông báo được hiển thị trước đó.
3hủy bỏ tất cả()

Phương thức này hủy tất cả các thông báo được hiển thị trước đó.
4thông báo (int id, Thông báo thông báo)
Phương thức này đăng thông báo được hiển thị trên thanh trạng thái.
5thông báo (Thẻ chuỗi, id int, Thông báo thông báo)

Phương thức này cũng đăng thông báo được hiển thị trên thanh trạng thái.

Thí dụ

Ví dụ dưới đây minh họa việc sử dụng lớp NotificationManager. Nó thùng một ứng dụng cơ bản cho phép bạn tạo một thông báo.

Để thử nghiệm với ví dụ này, bạn cần chạy ứng dụng này trên thiết bị thực hoặc trong trình giả lập.

Các bướcSự miêu tả
1Bạn sẽ sử dụng Android studio để tạo ứng dụng Android theo packagecom.example.sairamkrishna.myapplication.
2Sửa đổi tệp src / MainActivity.java để thêm mã Thông báo.
3Sửa đổi tệp XML bố trí res / layout / activity_main.xml thêm bất kỳ thành phần GUI nào nếu cần.
4Chạy ứng dụng và chọn một thiết bị Android đang chạy và cài đặt ứng dụng trên đó và xác minh kết quả.
Đây là nội dung của MainActivity.java .
Trong đoạn mã sau abc cho biết logo của tutorialspoint.com
package com.example.sairamkrishna.myapplication;

import android.app.Notification;
import android.app.NotificationManager;

import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;

import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {
   EditText ed1,ed2,ed3;
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      ed1=(EditText)findViewById(R.id.editText);
      ed2=(EditText)findViewById(R.id.editText2);
      ed3=(EditText)findViewById(R.id.editText3);
      Button b1=(Button)findViewById(R.id.button);

      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            String tittle=ed1.getText().toString().trim();
            String subject=ed2.getText().toString().trim();
            String body=ed3.getText().toString().trim();

            NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notify=new Notification.Builder
               (getApplicationContext()).setContentTitle(tittle).setContentText(body).
               setContentTitle(subject).setSmallIcon(R.drawable.abc).build();
                
               notify.flags |= Notification.FLAG_AUTO_CANCEL;
               notif.notify(0, notify);
         }
      });
   }
}
Đây là nội dung của activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Notification"
      android:id="@+id/textView"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="30dp" />
      .
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials Point"
      android:id="@+id/textView2"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:textSize="35dp"
      android:textColor="#ff16ff01" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText"
      android:layout_below="@+id/textView2"
      android:layout_alignLeft="@+id/textView2"
      android:layout_alignStart="@+id/textView2"
      android:layout_marginTop="52dp"
      android:layout_alignRight="@+id/textView2"
      android:layout_alignEnd="@+id/textView2"
      android:hint="Name" />
   
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText2"
      android:hint="Subject"
      android:layout_below="@+id/editText"
      android:layout_alignLeft="@+id/editText"
      android:layout_alignStart="@+id/editText"
      android:layout_alignRight="@+id/editText"
      android:layout_alignEnd="@+id/editText" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:inputType="textPersonName"
      android:ems="10"
      android:id="@+id/editText3"
      android:hint="Body"
      android:layout_below="@+id/editText2"
      android:layout_alignLeft="@+id/editText2"
      android:layout_alignStart="@+id/editText2"
      android:layout_alignRight="@+id/editText2"
      android:layout_alignEnd="@+id/editText2" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Notification"
      android:id="@+id/button"
      android:layout_marginTop="77dp"
      android:layout_below="@+id/editText3"
      android:layout_alignRight="@+id/textView"
      android:layout_alignEnd="@+id/textView" />

</RelativeLayout>
Đây là nội dung của AndroidManifest.xml .
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapplication" >
   
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name=".MainActivity"
         android:label="@string/app_name" >
         
         <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 chúng tôi. Để 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 biểu tượng từ thanh công cụ. 

Trước khi bắt đầu ứng dụng của bạn, Android studio sẽ hiển thị cửa sổ sau để chọn tùy chọn mà bạn muốn chạy ứng dụng Android của mình.

Bây giờ điền vào các lĩnh vực với tiêu đề, chủ đề và cơ thể. Điều này đã được hiển thị dưới đây trong hình

Bây giờ hãy nhấp vào nút thông báo và bạn sẽ thấy thông báo trong thanh thông báo trên cùng. Nó đã được hiển thị dưới đây

Bây giờ hãy cuộn xuống thanh thông báo và xem thông báo. Điều này đã được hiển thị dưới đây trong hình

Không có nhận xét nào:

Đăng nhận xét

Lập trình Android - RenderScript

Trong chương này, chúng ta sẽ tìm hiểu về Android RenderScript. Thông thường các ứng dụng trên Android được thiết kế để tiêu thụ tài nguyên ...