c5ec172e6fbc6eaa4c9071f71188b7d6b9854814
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / MyService.java
1 package ai.suanzi.rtmpclient;
2
3 import android.app.Service;
4 import android.content.Intent;
5 import android.os.Handler;
6 import android.os.HandlerThread;
7 import android.os.IBinder;
8 import android.os.Looper;
9 import android.util.Log;
10 import android.widget.Toast;
11 import android.support.v4.app.NotificationCompat;
12 import android.graphics.BitmapFactory;
13 import android.app.Notification;
14 import android.os.Message;
15
16 public class MyService extends Service {
17     private static final String TAG = "MyService";
18     private Ffmpeg ffmpeg = new Ffmpeg();
19     private  Boolean isRunning = false;
20
21     private Runnable runnable = new Runnable() {
22         @Override
23         public void run() {
24             Log.e(TAG, "Run ffmpeg");
25             isRunning = true;
26             ffmpeg.push(null);
27         }
28     };
29
30
31     /**
32      * id不可设置为0,否则不能设置为前台service
33      */
34     private static final int NOTIFICATION_DOWNLOAD_PROGRESS_ID = 0x0001;
35
36     //private boolean isRemove=false;//是否需要移除
37
38     /**
39      * Notification
40      */
41     public void createNotification(){
42         Log.e(TAG, "create notification");
43         //使用兼容版本
44         NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
45         //设置状态栏的通知图标
46         builder.setSmallIcon(R.mipmap.ic_launcher);
47         //设置通知栏横条的图标
48         builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_background));
49         //禁止用户点击删除按钮删除
50         builder.setAutoCancel(false);
51         //禁止滑动删除
52         builder.setOngoing(true);
53         //右上角的时间显示
54         builder.setShowWhen(true);
55         //设置通知栏的标题内容
56         builder.setContentTitle("Rtmp Foreground Service!!!");
57         //创建通知
58         Notification notification = builder.build();
59         //设置为前台服务
60         startForeground(NOTIFICATION_DOWNLOAD_PROGRESS_ID,notification);
61     }
62
63
64
65     @Override
66     public IBinder onBind(Intent intent) {
67         return null;
68     }
69
70     @Override
71     public void onCreate() {
72         super.onCreate();
73         Log.e(TAG, "onCreate");
74     }
75
76     @Override
77     public void onDestroy() {
78         stopForeground(true);
79         Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
80         Log.e(TAG, "onDestroy");
81         super.onDestroy();
82
83     }
84
85     @Override
86     public void onStart(Intent intent, int startid){
87         super.onStart(intent, startid);
88         Log.e(TAG, "onStart");
89
90     }
91
92     @Override
93     public int onStartCommand(Intent intent, int flags, int startId) {
94         Log.e(TAG, "onStartCommand");
95         if (!isRunning) {
96             createNotification();
97             Toast.makeText(this, "Ffmpeg started", Toast.LENGTH_LONG).show();
98             new Thread(runnable).start();
99         }
100
101 //        int i=intent.getExtras().getInt("cmd");
102 //        if(i==0){
103 //            if(!isRemove) {
104 //
105 //                createNotification();
106 //            }
107 //            isRemove=true;
108 //        }else {
109 //            //移除前台服务
110 //            if (isRemove) {
111 //                stopForeground(true);
112 //            }
113 //            isRemove=false;
114 //        }
115         //super.onStartCommand(intent, flags, startId);
116         return START_STICKY;
117     }
118
119     @Override
120     public void onLowMemory(){
121         super.onLowMemory();
122         Log.e(TAG, "onLowMemory");
123     }
124
125 }