-1

I want to call a function when phone is rebooted. I am starting a service but am unable to call a function of another class. Please help me.

I use the following code for this problem:

Broadcast Receiver:

public class BootComplete extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) { 
        if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
            Intent serviceIntent = new Intent(context, AutoStartUp.class);
            context.startService(serviceIntent);
        }
    }
}

Service:

public class AutoStartUp extends Service {
    MainActivity mainActivity;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        mainActivity.abc();
    }
}

Main Activity:

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(this, "Welcome1", Toast.LENGTH_LONG).show();
        abc();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void abc() {
        Toast.makeText(this, "Welcome", Toast.LENGTH_LONG).show();
    }
}

Manifest:

    <receiver
        android:name=".BootComplete"
        android:enabled="true"
        android:exported="false" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <service android:name=".AutoStartUp" >
    </service>

    <activity
        android:name="com.example.androidautostartup.MainActivity"
        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>

I am using the above code but still can't solve the problem.

Samurai
  • 3,724
  • 5
  • 27
  • 39
Gauri Shankar
  • 91
  • 3
  • 9

2 Answers2

0

Your code does not really make sense:

MainActivity mainActivity;

in your service, how is it initialized? Now it looks like it is null so:

mainActivity.abc();

results in NullPointerException.

Usually when you want to return some information from service to Activity you use local Broadcasts.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • so pleas tell me how call a class from a service class – Gauri Shankar May 13 '15 at 07:42
  • @GauriShankar to start activity from service: http://stackoverflow.com/questions/3606596/android-start-activity-from-service, to send information from service to activity (it must be started) : http://stackoverflow.com/questions/4461832/android-broadcast-from-service-to-activity. So you issue a broadcast from service, it is received in your activity, in the receiver you should call your function abc – marcinj May 13 '15 at 08:02
  • @marcinj what if he creates that function in broadcast receiver itself ? – Maveňツ May 13 '15 at 08:21
  • @maveň I assumed that if function abc() is inside activity then it probably is supposed to be placed there, ie. it changes widgets etc. – marcinj May 13 '15 at 08:32
0

You can't directly call a method of your Activity from a Service. For communication between e.g. a Service and an Activity Android provides BroadcastReceiver. The following tutorial is a good start.

In short you can use a BroadcastReceiver to send a command from your Service to your Activity like this:

Service side:

[...]
private void sendMessage() {
  Intent intent = new Intent("myEventCommunicator");
  intent.putExtra("message", "abc");
  LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
[...]

Activity side:

[...]
@Override
public void onResume() {
  super.onResume();

  // Register handler to receive messages.
  LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver,
      new IntentFilter("myEventCommunicator"));
}

// Create handler for receiving messages
private BroadcastReceiver messageReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    // Get data from message and do desired stuff
    String message = intent.getStringExtra("message");
    Log.d("Received message", "Message content: " + message);
    if(message.equals("abc") {
        abc();
    }
  }
};

private void abc() {
   // do some abc stuff...
}

@Override
protected void onPause() {
  // Unregister when app becomes paused
  LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
  super.onPause();
}
[...]

Further more you can start/resume your Activity like this from your Service:

    Intent i = new Intent();
    i.setClass(service.getBaseContext(), MyActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
            Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.putExtra("id", id + "");
    context.startActivity(i);

Your Application should be in launchMode = singleTop to bring a hidden instance (if exists) to the front or start a new instance if no old exists.

alex
  • 5,516
  • 2
  • 36
  • 60