alltolove 发表于 2017-10-27 08:56:03

android prgraming 8.2.1

新建个项目,修改activity_main.xml<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.xinwei.notificationtest.MainActivity">

    <Button
      android:id="@+id/send_notice"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="send notice"/>

</android.support.constraint.ConstraintLayout>

修改mainactivity.javapackage com.example.xinwei.notificationtest;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Button sendNotice=(Button)findViewById(R.id.send_notice);
      sendNotice.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
      Intent[] intent=new android.content.Intent[]{new Intent(this,NotificationActivity.class)};
      PendingIntent pi= PendingIntent.getActivities(this,0,intent,0);
      NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
      Notification notification=new NotificationCompat.Builder(this)
                .setContentTitle("This is content icon")
                .setContentText("This is content text")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pi)
                .setAutoCancel(true)
                .build();
      manager.notify(1,notification);

    }
}

再新建个NotificationActivity.java的空活动,把布局文件命名为notification_layout.xml,然后把布局文件修改为<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.xinwei.notificationtest.NotificationActivity">
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="This is notification layout"
      android:textSize="24sp"/>
</android.support.constraint.ConstraintLayout>

效果图:
页: [1]
查看完整版本: android prgraming 8.2.1