-1

I want to make start video playing automatically after android system is loaded. I have tried many different code snippets but doesn't worked for me. my android os version is 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>

Now it starts playing at startup and plays normally.

I changed code in PlayVideo.java, relocated media file in raw directory.

String path = "android.resource://" + getPackageName() + "/" + R.raw.y;
videoView.setVideoURI(Uri.parse(path));

and in Receive.java changed to:

public void onReceive(Context context, Intent intent) {
    Intent i = new Intent(context, PlayVideo.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
}
kobayashi
  • 35
  • 6

2 Answers2

1

The android.intent.action.BOOT_COMPLETED receiver is the right way to go and as far as I can see the is no big error in your code. Some suggestion though:

  • HTC devices might require com.htc.intent.action.QUICKBOOT_POWERON in the intent filter instead of android.intent.action.BOOT_COMPLETED so add both (i.e. having two <action android:name="...")
  • it's recommended to add the category android.intent.category.HOME
  • note that the BOOT_COMPLETED receiver only works, if the app is in status active. Check this nice SO post for details.
Community
  • 1
  • 1
Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • My phone is Motorola Droid 3 – kobayashi Apr 16 '15 at 12:59
  • Ok, so either your app isn't `active` or you need to add the `FLAG_ACTIVITY_NEW_TASK` flag (as Sheldon noted) or both ;) – Trinimon Apr 16 '15 at 13:08
  • Note that it's recommended to use `Environment.getExternalStorageDirectory() +"y.mp4"` instead of `"/mnt/sdcard/y.mp4"` and you have to add permissions for reading external storage (`android.permission.READ_EXTERNAL_STORAGE`). – Trinimon Apr 16 '15 at 13:11
  • I've added external storage, edited manifest file and getting error: The application has stopped unexpectedly and one another message : Sorry, this video cannot be played. I think storage is not ready for second one. – kobayashi Apr 16 '15 at 13:49
  • Please add a full stacktrace from your logcat in your question; otherwise it's hard to guess .... – Trinimon Apr 16 '15 at 13:58
0

This might help:

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

Also:

<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>

Depending on your device:

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

You may also need to look at using this instead of "/mnt/sdcard"

Environment.getExternalStorageDirectory().getPath()
Sheldon Neilson
  • 803
  • 4
  • 8