简体   繁体   English

Android:使用广播接收器错误将数据从服务发送到活动

[英]Android:send data from Service to activity with broadcastReceiver Error

I'm trying to send data from my service to the activity, but I'm getting an error:我正在尝试将数据从我的服务发送到活动,但出现错误:

SEND_DATA_INTENT cannot be resolved to a variable SEND_DATA_INTENT 无法解析为变量

This is the code I'm using.这是我正在使用的代码。 I hope someone will guide me to resolve this problem.我希望有人能指导我解决这个问题。

This onTaskCompleted method, from which I'll send data to activity这个 onTaskCompleted 方法,我将从中将数据发送到活动

public class DownloadService extends Service  implements OnTaskCompleted {
public void onTaskCompleted(String result){
            
            Log.i("receiving*******-----------",results+"");
            Intent intent = new Intent();//SEND_DATA_INTENTs
            intent.putExtra("type", "message");
            sendBroadcast(intent);
            
        }
}

This the main class, in which I created a brodcastReceiver class to receive data from Service:这是主类,我在其中创建了一个 brodcastReceiver 类来接收来自 Service 的数据:

 public class MainActivity extends Activity {
 private class DataReciver extends BroadcastReceiver
            {
                @Override
                public void onReceive(Context context, Intent intent)
                {
                    if (intent.getAction().equals(DownloadService.MSG_SET_INT_VALUE)) //
                    {
                        Bundle bdl = intent.getExtras();
                        String type = bdl.getString("type");
                        Log.i("DataReciver","/////"+type);
                        
                        
                    }
                }
            }
}

After a little search i found a solution : Binding to a Service.经过一番搜索,我找到了一个解决方案:绑定到服务。 And this is a working example , i hope it will help someone :这是一个工作示例,我希望它会帮助某人:

In the DownloadService class :service在 DownloadService 类中:服务

     public class DownloadService extends Service  implements OnTaskCompleted {
    private final IBinder mBinder = new LocalBinder();
         // Random number generator
        private final Random mGenerator = new Random();
     /**
             * Class used for the client Binder.  Because we know this service always
             * runs in the same process as its clients, we don't need to deal with IPC.
             */
            public class LocalBinder extends Binder {
                DownloadService getService() {
                    // Return this instance of LocalService so clients can call public methods
                    return DownloadService.this;
                }
            }
            @Override
            public IBinder onBind(Intent intent) {

                return mBinder;
            }
     /** method for clients */
            public int getRandomNumber() {
              return mGenerator.nextInt(100);
            }
}

This the main Activty :that will receive the number when clicking in the button :这是主要的活动:单击按钮时将收到号码:

public class MainActivity extends Activity {
 DownloadService mService;
        boolean mBound = false;
....
private ServiceConnection mConnection = new ServiceConnection() {
        // Called when the connection with the service is established
        public void onServiceConnected(ComponentName DownloadService, IBinder service) {
            // Because we have bound to an explicit
            // service that is running in our own process, we can
            // cast its IBinder to a concrete class and directly access it.
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e("TAG", "onServiceDisconnected");
            mBound = false;
        }
    };
     @Override
        protected void onStart() {
            super.onStart();
            // Bind to LocalService
            Intent intent = new Intent(this, DownloadService.class);
            bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        }

        @Override
        protected void onStop() {
            super.onStop();
            // Unbind from the service
            if (mBound) {
                unbindService(mConnection);
                mBound = false;
            }
        }

    public void onButtonClick(View v) {
        if (mBound) {
            // Call a method from the LocalService.
            // However, if this call were something that might hang, then this request should
            // occur in a separate thread to avoid slowing down the activity performance.
            int num = mService.getRandomNumber();

            Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
        }
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM