我有一项创建服务的活动,即使应用程序放在后台,此服务也负责播放视频并允许收听视频的音频。问题是,如何在服务中播放音频并允许应用程序在显示活动时在活动中显示视频?我的意思是,当应用程序在前台时,我需要显示的视频,当它在后台时我只需要音频,但不需要停止并重新启动视频。
我希望自己明白......
谢谢
塞巴斯蒂亚
答案 0 :(得分:0)
启用服务:
try {
Intent intent1 = new Intent(thisActivity, AlarmReciever.class);
Apps.sender = PendingIntent.getBroadcast(thisActivity, 2, intent1,
0);
Apps.am = (AlarmManager) getSystemService(ALARM_SERVICE);
long l = new Date().getTime();
if (l < new Date().getTime()) {
l += 100;
}
Apps.am.setRepeating(AlarmManager.RTC_WAKEUP, l, 100, sender);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_SHORT).show();
}
添加类AlarmReciver:
public class AlarmReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Play.check();
// Toast.makeText(context, "Alarm Triggered", Toast.LENGTH_LONG).show();
}
}
添加活动在其中播放并创建方法check()
@SuppressWarnings("deprecation")
public static void check() {
ActivityManager am1 = (ActivityManager) thisActivity
.getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am1.getRunningTasks(1).get(0).topActivity
.getPackageName();
if(packageName == "your app package name")
{
}
else
{
//if video play; show it
//else play audio
}
}
最后一件事添加了对mainfest的许可:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />