Android Notification自定义布局实现

    Android Notification的创建有两种方式,一种是采用默认的Notifiaction布局,一种是自定义布局——通常需要使用RemoteViews来实现。下面主要讲述自定义布局Notification实现:

    自定义布局Notification常见的场景是用于播放音乐时,在状态栏上有一些Button,用来控制音乐播放,下面是一个简单Demo,用来演示类似功能。

    大致功能是:当点击整个Notification时,会跳转到音乐界面;当点击Button时,弹出Toast,类似与调整音乐播放。

代码:

    主界面部分:

package com.natureday;


import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.widget.RemoteViews;
import android.widget.Toast;


public class MainActivity extends Activity {
public static final String TAG = "MainActivity";
public static final String CLICK_ACTION = "com.natureday.notification.click";

//用来接收点击Button时产生的广播。
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(CLICK_ACTION)) {
Log.i("TAG", "---------Click----------");
Toast.makeText(context, "Ha Ha !!!", Toast.LENGTH_SHORT).show();
}
}
};


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


IntentFilter filter = new IntentFilter();
filter.addAction(CLICK_ACTION);
this.registerReceiver(mReceiver, filter);


// Create RemoteViews Notification.
showRemoteViewNotification();


}

//创建包含Button的自定义布局Notification。
private void showRemoteViewNotification() {
// TODO Auto-generated method stub
RemoteViews views = new RemoteViews(getPackageName(),
R.layout.status_bar);

//这部分主要是对RemoteViews进行相关设定,关于Button的点击响应在这部分完成。
Intent intent = new Intent();
intent = new Intent(this.CLICK_ACTION);
PendingIntent pendingIntent = null;
pendingIntent = PendingIntent.getBroadcast(this,
0 /* no requestCode */, intent, 0 /* no flags */);

//根据不同组件的ID,为其指定不同PendingIntent——也就是当点击某一组件后,会发出不同的广播。
views.setOnClickPendingIntent(R.id.click, pendingIntent);

//这部分内容主要是对整个Notification进行相关设定。
Notification status = new Notification();
status.contentView = views;
status.flags |= Notification.FLAG_ONGOING_EVENT;
status.icon = R.drawable.ic_launcher;

//当点击Notification时会跳转到Music界面。
status.contentIntent = PendingIntent.getActivity(this, 0, new Intent(
"com.android.music.PLAYBACK_VIEWER")
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0);


NotificationManager manager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, status);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
this.unregisterReceiver(mReceiver);
}
}


自定义布局文件status_bar.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="@string/test_notification"
        android:textColor="#fffff000" />


    <Button
        android:id="@+id/click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="30dip"
        android:layout_toRightOf="@id/message"
        android:text="@string/btn_click" />


</RelativeLayout>


结果:演示结果是当点击Notification中的Button时,会弹出Toast,当点击Notification中Button以外的地方会跳转到音乐界面。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习是一种人工智能(AI)的子领域,致力于研究如何利用数据和算法让计算机系统具备学习能力,从而能够自动地完成特定任务或者改进自身性能。机器学习的核心思想是让计算机系统通过学习数据中的模式和规律来实现目标,而不需要显式地编程。 机器学习应用非常广泛,包括但不限于以下领域: 图像识别和计算机视觉: 机器学习在图像识别、目标检测、人脸识别、图像分割等方面有着广泛的应用。例如,通过深度学习技术,可以训练神经网络来识别图像中的对象、人脸或者场景,用于智能监控、自动驾驶、医学影像分析等领域。 自然语言处理: 机器学习在自然语言处理领域有着重要的应用,包括文本分类、情感分析、机器翻译、语音识别等。例如,通过深度学习模型,可以训练神经网络来理解和生成自然语言,用于智能客服、智能助手、机器翻译等场景。 推荐系统: 推荐系统利用机器学习算法分析用户的行为和偏好,为用户推荐个性化的产品或服务。例如,电商网站可以利用机器学习算法分析用户的购买历史和浏览行为,向用户推荐感兴趣的商品。 预测和预测分析: 机器学习可以用于预测未来事件的发生概率或者趋势。例如,金融领域可以利用机器学习算法进行股票价格预测、信用评分、欺诈检测等。 医疗诊断和生物信息学: 机器学习在医疗诊断、药物研发、基因组学等领域有着重要的应用。例如,可以利用机器学习算法分析医学影像数据进行疾病诊断,或者利用机器学习算法分析基因数据进行疾病风险预测。 智能交通和物联网: 机器学习可以应用于智能交通系统、智能城市管理和物联网等领域。例如,可以利用机器学习算法分析交通数据优化交通流量,或者利用机器学习算法分析传感器数据监测设备状态。 以上仅是机器学习应用的一部分,随着机器学习技术的不断发展和应用场景的不断拓展,机器学习在各个领域都有着重要的应用价值,并且正在改变我们的生活和工作方式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值