X-Git-Url: http://47.100.26.94:8080/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fai%2Fsuanzi%2Frtmpclient%2FMyService.java;fp=app%2Fsrc%2Fmain%2Fjava%2Fai%2Fsuanzi%2Frtmpclient%2FMyService.java;h=c5ec172e6fbc6eaa4c9071f71188b7d6b9854814;hb=3f9cc91f713654b667956ba0cc2d9d23669aab3a;hp=0000000000000000000000000000000000000000;hpb=a85b59e50709ca043c1464d3456dcd1abee84b8c;p=rtmpclient.git diff --git a/app/src/main/java/ai/suanzi/rtmpclient/MyService.java b/app/src/main/java/ai/suanzi/rtmpclient/MyService.java new file mode 100644 index 0000000..c5ec172 --- /dev/null +++ b/app/src/main/java/ai/suanzi/rtmpclient/MyService.java @@ -0,0 +1,125 @@ +package ai.suanzi.rtmpclient; + +import android.app.Service; +import android.content.Intent; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.IBinder; +import android.os.Looper; +import android.util.Log; +import android.widget.Toast; +import android.support.v4.app.NotificationCompat; +import android.graphics.BitmapFactory; +import android.app.Notification; +import android.os.Message; + +public class MyService extends Service { + private static final String TAG = "MyService"; + private Ffmpeg ffmpeg = new Ffmpeg(); + private Boolean isRunning = false; + + private Runnable runnable = new Runnable() { + @Override + public void run() { + Log.e(TAG, "Run ffmpeg"); + isRunning = true; + ffmpeg.push(null); + } + }; + + + /** + * id不可设置为0,否则不能设置为前台service + */ + private static final int NOTIFICATION_DOWNLOAD_PROGRESS_ID = 0x0001; + + //private boolean isRemove=false;//是否需要移除 + + /** + * Notification + */ + public void createNotification(){ + Log.e(TAG, "create notification"); + //使用兼容版本 + NotificationCompat.Builder builder=new NotificationCompat.Builder(this); + //设置状态栏的通知图标 + builder.setSmallIcon(R.mipmap.ic_launcher); + //设置通知栏横条的图标 + builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_background)); + //禁止用户点击删除按钮删除 + builder.setAutoCancel(false); + //禁止滑动删除 + builder.setOngoing(true); + //右上角的时间显示 + builder.setShowWhen(true); + //设置通知栏的标题内容 + builder.setContentTitle("Rtmp Foreground Service!!!"); + //创建通知 + Notification notification = builder.build(); + //设置为前台服务 + startForeground(NOTIFICATION_DOWNLOAD_PROGRESS_ID,notification); + } + + + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + @Override + public void onCreate() { + super.onCreate(); + Log.e(TAG, "onCreate"); + } + + @Override + public void onDestroy() { + stopForeground(true); + Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show(); + Log.e(TAG, "onDestroy"); + super.onDestroy(); + + } + + @Override + public void onStart(Intent intent, int startid){ + super.onStart(intent, startid); + Log.e(TAG, "onStart"); + + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + Log.e(TAG, "onStartCommand"); + if (!isRunning) { + createNotification(); + Toast.makeText(this, "Ffmpeg started", Toast.LENGTH_LONG).show(); + new Thread(runnable).start(); + } + +// int i=intent.getExtras().getInt("cmd"); +// if(i==0){ +// if(!isRemove) { +// +// createNotification(); +// } +// isRemove=true; +// }else { +// //移除前台服务 +// if (isRemove) { +// stopForeground(true); +// } +// isRemove=false; +// } + //super.onStartCommand(intent, flags, startId); + return START_STICKY; + } + + @Override + public void onLowMemory(){ + super.onLowMemory(); + Log.e(TAG, "onLowMemory"); + } + +}