Question

I'm trying to start an android service in an aspect. My project is basicly doing the following :

  • Sımple Activity has a method called callMethodA() and
  • In my Test.aj Aspect, I want to start my senderService just before callMethodA() called.

but, everytime I try to startService, it throws InvocationTargetException. Appearently, My problem is in my aspect, because honestly I have no idea how to start a service with an aspect.

Here's my Sender1Activity.java

 package com.example.sender;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;

    public class Sender1Activity extends Activity {
        Button btn_send;
        Intent serviceIntent;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_sender1);
            btn_send = (Button) findViewById(R.id.buttonSend);
        }


        public void callMethodA(View v){

            System.out.println("Method A called");
        }

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

    }

And Here's senderService.java :

    package com.example.sender;

import java.util.Random;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;

public class senderService extends Service {

    String value = String.valueOf("a");


    @Override
    public void onCreate() {
        super.onCreate();

    }

    public int onStartCommand(Intent intent, int flags, int startId) {



        mSendValue.removeCallbacks(hMyValueTask);
        mSendValue.post(hMyValueTask);
        return startId;
    }

    public Handler mSendValue = new Handler();
    public Runnable hMyValueTask = new Runnable() {
        public void run() {
            int n = new Random().nextInt(3000);
            System.out.println("A random delay : " + n);
            publishBuiltinAccelResults(value);
            mSendValue.postDelayed(hMyValueTask, (long) n);

        }
    };

    @Override
    public void onDestroy() {

    }

    public void publishBuiltinAccelResults(String value) {

        Intent intent = new Intent("ResultsA");
        intent.putExtra("resultA", value);
        sendBroadcast(intent);
    }



    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

}

And finally, here's the Test.aj Aspect code :

package com.example.sender;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public aspect Test extends Activity {
    int i = 1;

    protected void onCreate(Bundle savedInstanceState) {

    };

    pointcut pointcutCatchMethod() :  execution(* callMethodA(*))
     &&  within(com.example.sender.Sender1Activity);

    before() : pointcutCatchMethod() {
        Intent sendIntent = new Intent(this, senderService.class);
        startService(sendIntent);

        System.out.println(" sendBroadcast cought by Aspect , aspect No: " + i);

    }

}

Logcat :

03-07 15:20:27.015: E/AndroidRuntime(6714): FATAL EXCEPTION: main
03-07 15:20:27.015: E/AndroidRuntime(6714): java.lang.IllegalStateException: Could not execute method of the activity
03-07 15:20:27.015: E/AndroidRuntime(6714):     at android.view.View$1.onClick(View.java:3660)
03-07 15:20:27.015: E/AndroidRuntime(6714):     at android.view.View.performClick(View.java:4162)
03-07 15:20:27.015: E/AndroidRuntime(6714):     at android.view.View$PerformClick.run(View.java:17082)
03-07 15:20:27.015: E/AndroidRuntime(6714):     at android.os.Handler.handleCallback(Handler.java:615)
03-07 15:20:27.015: E/AndroidRuntime(6714):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-07 15:20:27.015: E/AndroidRuntime(6714):     at android.os.Looper.loop(Looper.java:137)
03-07 15:20:27.015: E/AndroidRuntime(6714):     at android.app.ActivityThread.main(ActivityThread.java:4867)
03-07 15:20:27.015: E/AndroidRuntime(6714):     at java.lang.reflect.Method.invokeNative(Native Method)
03-07 15:20:27.015: E/AndroidRuntime(6714):     at java.lang.reflect.Method.invoke(Method.java:511)
03-07 15:20:27.015: E/AndroidRuntime(6714):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
03-07 15:20:27.015: E/AndroidRuntime(6714):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
03-07 15:20:27.015: E/AndroidRuntime(6714):     at dalvik.system.NativeStart.main(Native Method)
03-07 15:20:27.015: E/AndroidRuntime(6714): Caused by: java.lang.reflect.InvocationTargetException
03-07 15:20:27.015: E/AndroidRuntime(6714):     at java.lang.reflect.Method.invokeNative(Native Method)
03-07 15:20:27.015: E/AndroidRuntime(6714):     at java.lang.reflect.Method.invoke(Method.java:511)
03-07 15:20:27.015: E/AndroidRuntime(6714):     at android.view.View$1.onClick(View.java:3655)
03-07 15:20:27.015: E/AndroidRuntime(6714):     ... 11 more
03-07 15:20:27.015: E/AndroidRuntime(6714): Caused by: java.lang.NullPointerException
03-07 15:20:27.015: E/AndroidRuntime(6714):     at android.content.ContextWrapper.startService(ContextWrapper.java:390)
03-07 15:20:27.015: E/AndroidRuntime(6714):     at com.example.sender.Test.ajc$before$com_example_sender_Test$1$61afa7eb(Test.aj:23)
03-07 15:20:27.015: E/AndroidRuntime(6714):     at com.example.sender.Sender1Activity.callMethodA(Sender1Activity.java:25)
03-07 15:20:27.015: E/AndroidRuntime(6714):     ... 14 more
Was it helpful?

Solution

an activity needs to be new'ed by android. you have 2 activities in this project, but only the main one is new'ed by android. the second one (the aspect) is not.

i would capture the target in the pointcut (Sender1Activity), and create a method there to start a service, since that is the only Activity with a valid context.

OTHER TIPS

I've solved the problem for now, thanks to aepurniet , Here's the solution, The pointcut in the aspect should be :

public aspect Test extends Activity{
    int i = 1;



    pointcut pointcutCatchMethod(Activity activity) :  execution(* callMethodA(*))
     && target(activity);


    before(Activity activity) : pointcutCatchMethod(activity) {

        Intent sendIntent = new Intent(activity.getApplicationContext(),senderService.class);

        activity.startService(sendIntent);

        System.out.println(" sendBroadcast cought by Aspect , aspect No: " + i);

    }




}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top