第二行代码学习笔记——第八章:丰富你的程序——运行手机多媒体

本章要点

Android强大的多媒体功能。


8.1 将手机运行到手机上

运行程序到手机上:先通过数据线把手机连接到电脑上。然后进入设置—>开发者选项界面,勾选这个界面中的USB选项。

注意:Android 4.2 系统开始,开发者选项默认是隐藏的。进入到“关于手机”界面,连续点击最下面的版本号,就会让开发者模式显示出来。

观察Android Monitor,你就会发现刚刚连接上的手机,如图:
am

运行当前项目,这时不会将程序运行到模拟器或手机上,而是弹出选择对话框,如下:
sdt

选择我们的手机,点击OK,就将程序运行到手机上了。


8.2 使用通知

通知(Notification)是Android中比较有特色的功能,当程序向用户发送提示信息,手机上方的状态栏就会显示一个通知图标,下拉就可以看到通知的详情内容,就连IOS在5.0版本之后也加入了类似的功能。

8.2.1 通知的基本用法

通知的使用方法,它可以在活动(较少),广播接收器和服务(较多)中创建。因为只有当程序进入后台我们才需要通知。

创建通知的步骤: 首先调用Context中的getSystemService()方法获取NotificationManager对通知进行管理。getSystemService()接收字符串(Context.NOTIFICATION.SERVICE),确定系统获取的是那个服务。
获取NotificationManager的实例如下:

NotificationManager manager=(NotificationManager)
getSystemService(Context.NOTIFICATION.SERVICE);

接下来使用support-v4中提供的NotificationCompat.Builder来创建Notification对象(兼容所有Android系统),代码如下:

Notification notification=new NotificationCompat.Builder(context).build();

在build()方法之前连缀设置方法创建一个Notification对象,最基本的设置:

Notification notification=new NotificationCompat.Builder(context)
         .setContentTitle("This is content title")
         .setContentText("This is content text")
         .setWhen(System.currentTimeMillis())
         .setSmallIcon(R.drawable.small_icon)
         .setLargeIcon(BitmapFactory.decodeResource(getResources(),
             R.drawable.large_icon))
         .build();

显示通知:调用NotificationManager中的notify()。接收两个参数,第一个参数id(保证每个通知指定的id都不同),第二个参数创建好的Notification对象。代码如下:

manager.notify(1,notification);

新建NotificationTest项目,修改activity_main.xml中的代码如下:

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

    <Button
        android:id="@+id/btn_send_notice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send notice"
        android:textAllCaps="false" />


</LinearLayout>

接下来修改MainActivity中的代码如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   

    private Button btn_send_notice;
    private NotificationManager manager;
    private Notification notification;

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

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_send_notice:
                manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                notification=new NotificationCompat.Builder(MainActivity.this)
                        .setContentTitle("This is content title")
                        .setContentText("This is content text")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                                R.mipmap.ic_launcher))
                        .build();
                manager.notify(1,notification);
                break;
            default:
        }
    }
}

运行程序,点击Send notice按钮,你会在系统状态栏看到一个小的图标,如图:
iconn
下拉系统状态栏可看到该通知的详情信息,如图:
contentn

实现通知点击效果:PendingIntent。
Intent和PendingIntent的区别:
共同点:启动活动,启动服务以及发送广播。
不同点:Intent更加倾向于立即执行某个动作,PendingIntent更加倾向于在某个合适的时机去执行某个动作。 PendingIntent为延迟执行的Intent。

PendingIntent提供了getActivity(),getBroadcast(),getService()静态方法来获取PendingIntent的实例(根据需求)。这几个方法接收的参数都是相同的,第一个参数Context。第二个参数传入0即可。第三个参数是Intent对象,通过这个对象构建出PendingIntent的”意图”。第四个参数确定PendingIntent的行为,有FLAG_ONE_SHOT,FLAG_NO_CREATE,FLAG_CANCEL_CURRENT,FALG_UPDATE_CURRENT这4中值,一般传入0即可。

NotificationCompat.Builder。构造器连缀setContentIntent()方法传入PendingIntent对象。

点击通知启动另一个活动。

创建NotificationActivity,布局起名为notification_layout。修改notification_layout.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:text="This is notification layout"
        android:layout_centerInParent="true"
        android:textSize="24sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

修改MainActivity中的代码如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   

    private Button btn_send_notice;
    private NotificationManager manager;
    private Notification notification;
    private PendingIntent pi;
    private Intent intent;

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

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_send_notice:
                intent=new Intent(this,NotificationActivity.class);
                pi=PendingIntent.getActivity(this,0,intent,0);
                manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                notification=new NotificationCompat.Builder(this)
                        .setContentTitle("This is content title")
                        .setContentText("This is content text")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                                R.mipmap.ic_launcher))
                        .setContentIntent(pi)
                        .build();
                manager.notify(1,notification);
                
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值