安卓 顶部通知栏 点击跳转不同页面

最近项目为了保活,在顶部添加了一个通知栏,查了官方api,具体做法如下:

1.定义顶部通知栏布局文件 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp" >
    <ImageView android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp" />

    <LinearLayout
        android:layout_toRightOf="@id/image"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_centerVertical="true"
        android:layout_height="wrap_content">

        <TextView android:id="@+id/title"
            android:text="这是标题"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            />

        <TextView android:id="@+id/text"
            android:layout_width="wrap_content"
            android:text="这是内容"
            android:layout_marginTop="10px"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/image"
            android:layout_below="@id/title"
            />

    </LinearLayout>

    <!--style="@style/NotificationTitle" -->

    <!--style="@style/NotificationText"-->
</RelativeLayout>

2.定义主页面布局文件

<?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.syw.myapplication.MainActivity">

    <Button
        android:onClick="notify"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Hello World!" />

</android.support.constraint.ConstraintLayout>

3.MainActivity代码

package com.syw.myapplication;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;

public class MainActivity extends Activity {

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

    public void notify(View view) {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.mipmap.ic_launcher, "Hello", System.currentTimeMillis());

        //syw 点击跳转的页面
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.contentIntent = contentIntent;

        //syw 自定义的页面
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
        contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
        contentView.setTextViewText(R.id.title, "Custom notification");
        contentView.setTextViewText(R.id.text, "This is a custom layout");
        notification.contentView = contentView;

        //syw 自定义页面点击事件
//        Intent intent2=new Intent(this,MainActivity.class);
//        PendingIntent pendingIntent2=PendingIntent.getActivity(this, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
//        给我remoteViews上的控件tv_content添加监听事件
//        remoteViews.setOnClickPendingIntent(R.id.title, pendingIntent2);

        mNotificationManager.notify(111, notification);
    }
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值