Android:使用broadcastReceiver Error将数据从Service发送到活动

时间:2014-04-22 17:01:41

标签: android service android-activity broadcastreceiver

我正在尝试将数据从我的服务发送到活动,但我收到错误:

SEND_DATA_INTENT无法解析为变量

这是我正在使用的代码,希望有人能帮我解决这个问题。谢谢你提前。 这个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);

        }
}

这是主类,我创建了一个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);


                    }
                }
            }
}

1 个答案:

答案 0 :(得分:0)

经过一番搜索,我找到了一个解决方案:绑定到服务。 这是一个有效的例子,我希望它会帮助某人:

在DownloadService类:service

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

这是主要的Activty:点击按钮时会收到该号码:

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


}