gpt4 book ai didi

java - Android启动Service的一个方面

转载 作者:行者123 更新时间:2023-12-02 06:06:19 24 4
gpt4 key购买 nike

我正在尝试在某个方面启动 android 服务。我的项目基本上做了以下工作:

  • Sımple Activity 有一个名为 callMethodA() 的方法和
  • 在我的Test.aj方面,我想在调用callMethodA()之前启动我的senderService .

但是,每次我尝试启动Service 时,它​​都会抛出 InitationTargetException。显然,我的问题出在我的方面,因为老实说,我不知道如何从方面启动服务。

这是我的 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;
}

}

这是 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;
}

}

最后,这是 Test.aj Aspect 代码:

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

}

}

日志猫:

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

最佳答案

一个 Activity 需要由 android 来新建。您在此项目中有 2 个 Activity ,但只有主要 Activity 是由 android 新建的。第二个(方面)不是。

我将捕获切入点 (Sender1Activity) 中的目标,并在那里创建一个方法来启动服务,因为这是唯一具有有效上下文的 Activity。

关于java - Android启动Service的一个方面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22252300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com