java - 将类对象传递给服务

标签 java android

我是 android 和 java 的新手,遇到了一个问题。我有一个方法 Activity ,我想在后台使用它们。对于背景,我使用了服务类。问题是我不能将 Activity 对象传递给服务构造函数,因为它不能包含任何参数。我阅读了如何通过 intend 发送字符串和 double ,但我找不到如何通过 Activity 类(class)。

public class MainActivity extends Activity
{
    @Override
    protected void onCreate( Bundle savedInstanceState ){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onStop(){
        super.onStop();
        startService(new Intent(this, MyService.class));
    }

    @Override
    public void onResume(){
        super.onResume();
        stopService(new Intent(this, MyService.class));
    }

    public void toast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}

//---------------------------------------

public class MyService extends Service
{
    private MainActivity main;

    public MyService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        main.toast("test");    // <- here I want to use some method
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

感谢您的帮助!

最佳答案

您好,请看下面的步骤一定能帮到您。

第 1 步。定义您的服务将用于传达事件的接口(interface):

public interface ServiceCallbacks {
    void Showtoast(String msg);
} 

第 2 步。 编写您的服务类。您的 Activity 将绑定(bind)到此服务。此外,我们将添加一个方法来设置 ServiceCallbacks。

public class MyService extends Service {
    // Binder given to clients 
    private final IBinder binder = new LocalBinder();
    // Registered callbacks 
    private ServiceCallbacks serviceCallbacks;


    // Class used for the client Binder. 
    public class LocalBinder extends Binder {
        MyService getService() { 
            // Return this instance of MyService so clients can call public methods 
            return MyService.this;
        } 
    } 

    @Override 
    public IBinder onBind(Intent intent) {
        return binder;
    } 

    public void setCallbacks(ServiceCallbacks callbacks) {
        serviceCallbacks = callbacks;
    } 
} 

第 3 步。 按照相同的指南编写您的 Activity 类,但还要使其实现您的 ServiceCallbacks 接口(interface)。当您绑定(bind)/取消绑定(bind)服务时,您将通过调用服务上的 setCallbacks 来注册/取消注册它。

public class MyActivity extends Activity implements ServiceCallbacks {
    private MyService service;
    private boolean bound = false;

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

    @Override 
    protected void onStart() { 
        super.onStart(); 
        // bind to Service 
        Intent intent = new Intent(this, MyService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    } 

    @Override 
    protected void onStop() { 
        super.onStop(); 
        // Unbind from service 
        if (bound) {
            service.setCallbacks(null); // unregister
            unbindService(serviceConnection);
            bound = false;
        } 
    } 

    /** Callbacks for service binding, passed to bindService() */ 
    private ServiceConnection serviceConnection = new ServiceConnection() {

        @Override 
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // cast the IBinder and get MyService instance 
            LocalBinder binder = (LocalBinder) service;
            service = binder.getService();
            bound = true;
            service.setCallbacks(this); // register
        } 

        @Override 
        public void onServiceDisconnected(ComponentName arg0) {
            bound = false;
        } 
    }; 

    /* Defined by ServiceCallbacks interface */ 
    @Override 
    public void Showtoast(String message) { 
         Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    } 
} 

谢谢,如果有任何问题,请告诉我。

关于java - 将类对象传递给服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29649913/

相关文章:

php - 如何将我自己的日历与我的手机(iPhone、Android 等)同步

java - Android 通知设置ContentView

java - RandomAccessFile 查找和写入或使用偏移量写入的意外行为

java - 为java应用程序的执行保留一个计数器

java - Android 表格单元格边框

java - JacocoReport 配置的 Gradle 6.0 弃用警告

java - 需要访问 setOnClickListener 方法中的变量

java - 如何使用displaytag过滤数据?

java - 将 Int 值转换为 String 值

java - 尝试在Unity3d中构建Android版本时遇到问题