Ronak Mehta
Ronak Mehta

Reputation: 5979

start Activity at startup of phone in android

I am trying to start activity at start up of phone but whole program is not running there is a no error in program , see my coding bellow(or here http://pastebin.com/BKaE4AaU):

autostart.java

import android.content.BroadcastReceiver;  
import android.content.Context;  
import android.content.Intent;  
import android.content.SharedPreferences;  
import android.preference.PreferenceManager;  
import android.util.Log;


public class autostart extends BroadcastReceiver 
{
    public void onReceive(Context arg0, Intent arg1) 
    {
        Intent intent = new Intent(arg0,service.class);
        arg0.startService(intent);
        Log.i("Autostart", "started");
    }
}

service.java

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class service extends Service
{
    private static final String TAG = "MyService";
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
    }

    @Override
    public void onStart(Intent intent, int startid)
    {
        Intent intents = new Intent(getBaseContext(),hello.class);
        intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intents);
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");
    }
}

hello.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class hello extends Activity 
{   
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Toast.makeText(getBaseContext(), "Hello........", Toast.LENGTH_LONG).show();
    }
}

manifest.java is as follow

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <receiver  android:name=".autostart"
              android:label="@string/app_name">
        <intent-filter>
              <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

Upvotes: 2

Views: 5651

Answers (2)

Peter Knego
Peter Knego

Reputation: 80330

So when device boots up you'd like your activity to pop-up and show itself to the user without user's action?

Please don't do that - that's a very bad UX practice. Do you know of any existing apps that do that? I don't know of any. I personally would not use such app.

If you need to notify user about something then use the system preferred way - notifications.

Upvotes: 0

Frank Harper
Frank Harper

Reputation: 3176

Your XML should be stored in a file named AndroidManifest.xml, not manifest.java.

Another reason your code is not being run, might be that your App is installed on external storage (sdcard). BOOT_COMPLETE is sent to applications before external storage is mounted. So if application is installed to external storage it won't receive BOOT_COMPLETE broadcast message.

If that isn't the problem, there is already a very good description of how to get boot completed receivers working on Android.

Upvotes: 5

Related Questions