用服务启动尝试(不行)

nx_mplive
liuguijing 2 months ago
parent 81f2c074d2
commit 27809fd87b

@ -3,7 +3,7 @@ apply plugin: 'com.android.application'
// 10,00,000 major-minor-build
def AppMajorVersion = 1
def AppMinorVersion = 0
def AppBuildNumber = 4
def AppBuildNumber = 5
def AppVersionName = AppMajorVersion + "." + AppMinorVersion + "." + AppBuildNumber
def AppVersionCode = AppMajorVersion * 100000 + AppMinorVersion * 1000 + AppBuildNumber

@ -9,7 +9,7 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
@ -30,6 +30,11 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".CameraForegroundService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="camera" />
</application>
</manifest>

@ -0,0 +1,81 @@
package com.xypower.mplive;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
public class CameraForegroundService extends Service {
private static final int NOTIFICATION_ID = 123;
private static final String CHANNEL_ID = "camera_service";
@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 创建必须的通知Android 8.0+要求)
Notification notification = buildCameraNotification();
// 启动为前台服务
startForeground(NOTIFICATION_ID, notification);
// 在这里可以初始化摄像头
initCameraInService();
return START_STICKY;
}
private Notification buildCameraNotification() {
// 创建点击跳转到应用的PendingIntent
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
return new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("摄像头服务运行中")
.setContentText("正在使用摄像头功能")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.build();
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"摄像头服务通道",
NotificationManager.IMPORTANCE_HIGH);
serviceChannel.setDescription("用于摄像头服务的通知通道");
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
void sendDataToActivity() {
Intent intent = new Intent("ACTION_UPDATE_UI");
intent.putExtra("key", "value");
sendBroadcast(intent);
}
private void initCameraInService() {
// 在这里实现摄像头初始化逻辑
// 注意:部分设备可能仍限制服务中直接访问摄像头
sendDataToActivity();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

@ -1,7 +1,10 @@
package com.xypower.mplive;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
@ -71,6 +74,9 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
private Handler mHandler;
private int cameraId = 0;
private List<CameraItemData> cameraData;
private int autoStart;
private EditText efu;
private int rotation;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -82,6 +88,14 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
setContentView(R.layout.activity_main);
// response screen rotation event
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
registerReceiver(updateReceiver, new IntentFilter("ACTION_UPDATE_UI"));
// 启动前台服务
Intent serviceIntent = new Intent(this, CameraForegroundService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(serviceIntent);
} else {
startService(serviceIntent);
}
requestPermission();
}
@ -124,18 +138,27 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
}
}
}
private BroadcastReceiver updateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ("ACTION_UPDATE_UI".equals(action)) {
String data = intent.getStringExtra("key");
extracted(autoStart, efu, rotation);
}
}
};
private void init() {
Intent intent = getIntent();
final int autoStart = intent.getIntExtra("autoStart", 1);
autoStart = intent.getIntExtra("autoStart", 1);
// restore data.
sp = getSharedPreferences("MpLive", MODE_PRIVATE);
// rtmpUrl = sp.getString("rtmpUrl", rtmpUrl);
// initialize url.
final EditText efu = (EditText) findViewById(R.id.url);
efu = (EditText) findViewById(R.id.url);
btnPublish = (Button) findViewById(R.id.publish);
@ -155,7 +178,7 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
}
efu.setText(rtmpUrl);
mCameraView = (SrsCameraView) findViewById(R.id.glsurfaceview_camera);
final int rotation = intent.getIntExtra("rotation", -1);
rotation = intent.getIntExtra("rotation", -1);
if (rotation != -1) {
//设置图像显示方向
mCameraView.setPreviewOrientation(rotation);
@ -188,33 +211,9 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
if (autoStart != 0) {
startRTMPServer();
mPublisher.switchCameraFace(cameraId,rotation);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// rtmpUrl = "rtmp://61.169.135.146/live/0";
SharedPreferences.Editor editor = sp.edit();
editor.putString("rtmpUrl", rtmpUrl);
editor.apply();
efu.setText(rtmpUrl + "rotation= " + rotation + " cameraid=" + cameraId + " auto=" + autoStart);
// efu.setText(rtmpUrl + " cameraid=" + cameraId + " auto=" + autoStart);
efu.setText(rtmpUrl);
mPublisher.startPublish(rtmpUrl);
if (btnSwitchEncoder.getText().toString().contentEquals("soft encoder")) {
// Toast.makeText(getApplicationContext(), "Use hard encoder", Toast.LENGTH_SHORT).show();
// extracted(autoStart, efu, rotation);
} else {
Toast.makeText(getApplicationContext(), "Use soft encoder", Toast.LENGTH_SHORT).show();
}
btnPublish.setText("stop");
btnSwitchEncoder.setEnabled(false);
btnPause.setEnabled(true);
}
}, 500);
} else {
mPublisher.switchCameraFace(cameraId,rotation);
mPublisher.switchCameraFace(cameraId, rotation);
}
int autoClose = intent.getIntExtra("autoClose", 0);
@ -243,7 +242,7 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
efu.setText(rtmpUrl);
mPublisher.startPublish(rtmpUrl);
// mPublisher.startCamera();
mPublisher.switchCameraFace(cameraId,rotation);
mPublisher.switchCameraFace(cameraId, rotation);
if (btnSwitchEncoder.getText().toString().contentEquals("soft encoder")) {
Toast.makeText(getApplicationContext(), "Use hard encoder", Toast.LENGTH_SHORT).show();
@ -282,7 +281,7 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
int size = cameraData.size();
if (size > 0) {
int i = (++cameraId) % size;
mPublisher.switchCameraFace(cameraId,rotation);
mPublisher.switchCameraFace(cameraId, rotation);
}
}
});
@ -318,6 +317,32 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
});
}
private void extracted(final int autoStart, final EditText efu, final int rotation) {
startRTMPServer();
mPublisher.switchCameraFace(cameraId, rotation);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// rtmpUrl = "rtmp://61.169.135.146/live/0";
SharedPreferences.Editor editor = sp.edit();
editor.putString("rtmpUrl", rtmpUrl);
editor.apply();
efu.setText(rtmpUrl + "rotation= " + rotation + " cameraid=" + cameraId + " auto=" + autoStart);
// efu.setText(rtmpUrl + " cameraid=" + cameraId + " auto=" + autoStart);
efu.setText(rtmpUrl);
mPublisher.startPublish(rtmpUrl);
if (btnSwitchEncoder.getText().toString().contentEquals("soft encoder")) {
// Toast.makeText(getApplicationContext(), "Use hard encoder", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Use soft encoder", Toast.LENGTH_SHORT).show();
}
btnPublish.setText("stop");
btnSwitchEncoder.setEnabled(false);
btnPause.setEnabled(true);
}
}, 500);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
@ -399,6 +424,12 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
// }
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(updateReceiver);
}
@Override
protected void onResume() {
super.onResume();
@ -416,6 +447,7 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
@Override
protected void onDestroy() {
super.onDestroy();
stopRTMPServer();
mPublisher.stopPublish();
mPublisher.stopRecord();
}

Loading…
Cancel
Save