本文笔者在青岛游玩的时候然突想到的...近期就有想写几篇关于服务通知的博客,所以回家到之后就奋笔疾书的写出来发布了

    Notification除了用于台后服务通知,还常用在面下情况:

    (1)坚持服务存在。当统系存内不足时,统系会为认某台后服务占用存内间时太长而中断该服务,以释放存内。对于某些服务,例如播放音乐,如果统系对该服务停止源资释放,户用体验就成了音乐然突没有声音。对这类服务,我们希望享有更高的优先级别,不会被统系干掉。

    (2)户用随时与服务停止互动。例如播放音乐的服务,户用可随时暂停音乐播放,或选择其他曲目,甚至中断播放音乐服务。

    要实现上述两点,法方是在Service中称宣自己是foreground,并持维一个通知来向户用标明foreground状态,户用可以通过下拉通知并点击通知,进入该服务界面,实行互动作操。

    我们将在Android习学记笔(五二):服务Service(中)- 承继Service类的模拟音乐播放例子上停止扩展。

    服务通知Android学习笔记(五五):通知Notification(下)_播放音乐

    FakePlayer同原例子的客户端码代,略过不表。Service的码代如下


    每日一道理

宽容,是一种坦荡,可以无私无畏,无拘无束,无尘无染。宽容,是一种豁达,是比海洋和天空更为博大的胸襟,是宽广和宽厚的叠加,延续和升华。宽容有度,宽容无价,宽以待人,这是人生处世的基本法则。


    

public class FakePlayerService extends Service{

    public static final String EXTRA_PLAYLIST="EXTRA_PLAYLIST";

    public static final String EXTRA_SHUFFLE="EXTRA_SHUFFLE";

    private boolean isPlay = false;

    private static final int NOTIFY_FAKEPLAYER_ID=1339; 

    

    public IBinder onBind(Intent arg0) { 

        return null;

    }


    public void onDestroy() { 

        stop();

    }


    public int onStartCommand(Intent intent, int flags, int startId) { 

        String playlist = intent.getStringExtra(EXTRA_PLAYLIST);

        Boolean isShuffle = intent.getBooleanExtra(EXTRA_SHUFFLE, false);

        play(playlist,isShuffle);

        return START_NOT_STICKY;

    }

   

    private void play(String playlist, Boolean isShuffle){ 

        if(!isPlay){ 

            isPlay = true;

            //和上一记笔中创立通知的骤步一样,只是不须要通过通知管理器停止触发,而是用startForeground(ID,notify)来处置

            //骤步1:和上一记笔一样,通过Notification.Builder( )来创立通知

            //FakePlayer就是两个大button的activity,也即服务的界面,见最左图

            Intent i = new Intent(this,FakePlayer.class);

            //注意Intent的flag设置:FLAG_ACTIVITY_CLEAR_TOP: 如果activity已在当前任务中运行,在它前端的activity都会被闭关,它就成了最前端的activity。FLAG_ACTIVITY_SINGLE_TOP: 如果activity已经在最前端运行,则不须要再加载。设置这两个flag,就是让一个且独一的一个activity(服务界面)运行在最前端。

            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);            PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);           

            Notification myNotify = new Notification.Builder(this)

                                    .setSmallIcon(R.drawable.shield)

                                    .setTicker("Fake Player: " + playlist)

                                    .setContentTitle("Test")

                                    .setContentText("Now Playing: \"Ummmm, Nothing\"")

                                    .setContentIntent(pi)

                                    .build();            

            //设置notification的flag,标明在点击通知后,通知并不会失消,也在最右图上仍在通知栏示显图标。这是确保在activity中退出后,状态栏仍有图标可提下拉、点击,再次进入activity。

            myNotify.flags |= Notification.FLAG_NO_CLEAR;

           

           // 骤步 2:startForeground( int, Notification)将服务设置为foreground状态,使统系知道该服务是户用存眷,低存内情况下不会killed,并供给通知向户用标明处于foreground状态。
            startForeground
(NOTIFY_FAKEPLAYER_ID,myNotify);

        }

    }

   

    private void stop(){

        if(isPlay){

            isPlay = false;

           //将服务从forefround状态中移走,使得统系可以在低存内的情况下除清它。 
            stopForeground
(true); 

        }

    }


}