加载android系统后启动应用程序

时间:2015-04-16 12:30:29

标签: android

我想在加载android系统后自动启动视频播放。我尝试了很多不同的代码片段,但对我来说并不起作用。我的android os版本是2.3.4。

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);
}

2 个答案:

答案 0 :(得分:1)

android.intent.action.BOOT_COMPLETED接收器是正确的方法,据我所知,代码中没有大的错误。但有些建议:

  • HTC设备在意图过滤器中可能需要com.htc.intent.action.QUICKBOOT_POWERON而不是android.intent.action.BOOT_COMPLETED,因此请同时添加(即有两个<action android:name="..."
  • 建议添加类别android.intent.category.HOME
  • 请注意,BOOT_COMPLETED接收器仅在应用状态处于活动状态时才有效。有关详细信息,请查看此nice SO post

答案 1 :(得分:0)

这可能会有所帮助:

public class Receive extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, PlayVideo.class);  
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i); 
    }
}

此外:

<receiver
    android:name=".Receive"
    android:enabled="true"
    android:exported="false">
    <intent-filter android:priority="100">
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <!--QUICKBOOT_POWERON for HTC-->
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

取决于您的设备:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

您可能还需要使用此代替“/ mnt / sdcard”

Environment.getExternalStorageDirectory().getPath()