Android 使服务关闭外部活动(通过隐式意图创建)
我想做的很简单:我的应用程序允许用户最多观看10分钟的视频,然后停止视频并返回到我的应用程序(上一个活动)。视频显示在带有以下代码的外部播放器中:Android 使服务关闭外部活动(通过隐式意图创建),android,service,android-activity,Android,Service,Android Activity,我想做的很简单:我的应用程序允许用户最多观看10分钟的视频,然后停止视频并返回到我的应用程序(上一个活动)。视频显示在带有以下代码的外部播放器中: Intent intentVideo = new Intent(); intentVideo.setAction(Intent.ACTION_VIEW); intentVideo.setData(Uri.parse(url))); startActivity(intentVideo); 然后后台服务定期检查时间是否已过 我的服务如何终止视频活动(
Intent intentVideo = new Intent();
intentVideo.setAction(Intent.ACTION_VIEW);
intentVideo.setData(Uri.parse(url)));
startActivity(intentVideo);
然后后台服务
定期检查时间是否已过
我的服务如何终止视频活动(,因为它是由外部应用程序提供的,所以我无法添加代码或侦听器,或者其他什么),并在时间过去后使我的应用程序返回到以前的活动
谢谢解决此问题的一种方法是在
活动
中定义一个广播接收器
。当服务
需要通知活动
时间到了时,发送广播并在广播接收器
中接收。然后,在onReceive()
中调用Activity
上的finish()。希望这能有所帮助。好的,这是我的最终代码,如果它能有所帮助的话,感谢Egor
注意:有两种解决方案可以强制停止玩家活动:
使用startActivityForResult(intent,rq)
/finishActivity(rq)
使用FLAG\u ACTIVITY\u CLEAR\u TOP
请小心使用finishActivity()
,某些外部应用程序不会因为其行为而关闭。对我来说,当我用VLC播放器打开视频时效果很好,但当我用Dailymotion应用程序打开视频时效果不好
启动splayer.java的活动
TimeWatcher.java
当你说“活动中的广播接收器”时,你指的是什么活动?因为前台活动是视频播放器,所以我无法更改代码以添加广播接收器。如果我在上一个活动中添加了一个BroadcastReceiver,我想它不会收到任何东西,因为它不再显示此活动了?Flox Egor说的是一个确切的答案,你说不能在你的应用程序中添加代码是什么意思,这并不意味着senseI不明白我应该如何以及在哪里调用finish()。BroadcastReceiver不是应该在前台活动中,即视频播放器活动(外部应用程序)中吗?因为运行视频活动(因此可以停止它)的活动已暂停。我猜Broadcast Receiving在暂停状态下不工作?@Flox,Broadcast Receiver即使在封闭活动处于后台时也会工作。BroadcastReceiver可以很容易地成为一个单独的类,但将其设计为活动的内部类可以很容易地访问onReceive()中的活动,并调用该活动的finish()。很抱歉,它对我不起作用。玩家永远不会被处决。
public class ActivityThatLaunchesPlayer extends Activity
{
private BroadcastReceiver brdreceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
System.out.println("broadcast signal received");
//either
finishActivity(57); //57 is my arbitrary requestcode
//or either :
Intent intentback = new Intent(getApplicationContext(), ActivityThatLaunchesPlayer.class);
intentback.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentback);
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//set the brdcstreceiver to listen to the slot
getApplicationContext().registerReceiver(brdreceiver, new IntentFilter("com.example.portail10.timeElapsed"));
//here we launch the player (android opens a new appropriate activity)
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse(uri));
startActivityForResult(intent, 57); //again arbitrary rqstcode
//here we start the service that watch the time elapsed watching the video
intentServ = new Intent(this, TimeWatcher.class);
startService(intentServ);
}
}
public class TimeWatcher extends Service
{
//... some code is missing, but the main idea is here
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
//send the broadcast when time's up
Intent intentbrd = new Intent();
intentbrd.setAction("com.example.portail10.timeElapsed");
sendBroadcast(intentbrd);
System.out.println("Brdcast sent");
stopSelf();
}
}, 0, 600000); //in ms = 10min
return START_NOT_STICKY;
}