我想在Android系統加載後自動啓動視頻播放。我嘗試了很多不同的代碼片段,但不適合我。我的Android操作系統版本是2.3.4。加載Android系統後啓動應用程序
Receive.java
package com.android.fireup;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Receive extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startActivity(new Intent(context, PlayVideo.class));
}
}
PlayVideo.java
package com.android.fireup;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.VideoView;
public class PlayVideo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_video);
String path = "android.resource://" + getPackageName() + "/" + R.raw.y;
VideoView videoView = (VideoView) this.findViewById(R.id.myVideo);
videoView.setVideoURI(Uri.parse(path));
//videoView.setVideoPath("/mnt/sdcard/y.mp4");
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.setLooping(true);
}
});
videoView.requestFocus();
videoView.start();
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.fireup"
android:installLocation="internalOnly">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-sdk android:minSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name=".Receive"
android:enabled="true"
android:exported="false">
<intent-filter android:priority="100">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity
android:name=".PlayVideo"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
現在它在啓動時開始播放並正常播放。
我在原料目錄改變碼PlayVideo.java,搬遷的媒體文件。
String path = "android.resource://" + getPackageName() + "/" + R.raw.y;
videoView.setVideoURI(Uri.parse(path));
和Receive.java改爲:
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, PlayVideo.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
它說不能播放視頻。 後此我改變行: <動機器人:名稱= 「android.intent.action.BOOT_COMPLETED」/> 到 <操作機器人:名稱= 「android.intent.action.DEVICE_STORAGE_OK」/> 但沒有。 我使用內部存儲。 – dimmed
如果您正常啓動應用程序,它會播放嗎? –
是的,它播放正常 – dimmed