我有一个活动要用于显示从服务发送的数据.每当服务向活动发送消息时,活动都会更新textvIEw.用户可以使用活动中的按钮来启动和停止服务(服务由用户明确启动/停止).目前,我的活动已绑定到onResume()中的服务,并取消了在onDestroy()中的绑定,而该服务似乎目前正在工作.
当我启动服务,按“后退”按钮,然后尝试再次聚焦活动时,会出现问题.我面临着一个新活动,该活动不会被服务更新,但似乎能够将消息发送到服务(我从处理程序中的Logs中注意到了这一点[男孩,这句话使我发笑了!]).理想情况下,我只希望活动的一个实例存在并保持与服务的绑定,但是我绝对不希望看到活动与更新保持同步.我已经用< activity android:launchMode =“ singletop”>更新了清单,但不确定是否可以帮上大忙.在以后恢复活动时,我该怎么做以确保我的活动重新绑定到该服务的现有实例? (我想通过消息传递来实现此目的,我不想实现数据库或为此目的使用sharedprefereces).我在下面添加了我的代码-我对服务来说还比较陌生,所以请放轻松.干杯.
可选的额外:
我的目标是从startService()连续运行的服务,在该活动中重新聚焦活动是可选的,但是会立即显示该服务的最新信息.我相信我正在寻找的是前台服务.这不是问题的一部分,但是如果您认为您的答案可能会阻止我实现此目标,请告诉我.
MainActivity.java:
public class MainActivity extends Activity { private static final String deBUG = "MainActivity"; private boolean bound = false; TextVIEw mTextVIEw; protected Messenger outMessenger; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); Log.i(deBUG, "onCreate called"); mTextVIEw = (TextVIEw) findVIEwByID(R.ID.textVIEw1); button bind = (button) findVIEwByID(R.ID.bind); button unbind = (button) findVIEwByID(R.ID.unbind); bind.setonClickListener(new OnClickListener() { public voID onClick(VIEw v) { Message message = new Message(); Bundle bundle = new Bundle(); bundle.putString("ACTION", "START"); message.setData(bundle); try { outMessenger.send(message); } catch (remoteexception e) { Log.d(deBUG, "She can't reach the service captain!"); } } }); unbind.setonClickListener(new OnClickListener() { public voID onClick(VIEw v) { Message message = new Message(); Bundle bundle = new Bundle(); bundle.putString("ACTION", "Stop"); message.setData(bundle); try { outMessenger.send(message); } catch (remoteexception e) { Log.d(deBUG, "She can't reach the service captain!"); } } }); } @OverrIDe public voID onPause() { super.onPause(); } @OverrIDe public voID onResume() { super.onResume(); dobindService(); } public voID onDestroy() { super.onDestroy(); Log.i(deBUG, "onDestroy Called"); doUnbindService(); } private voID dobindService() { Log.i(deBUG, "Attempting to bind"); bound = true; Intent i = new Intent(this, MyService.class); i.putExtra("MESSENGER", new Messenger(mHandler)); bindService(i, mConnection, Context.BIND_auto_CREATE); } private voID doUnbindService() { Log.i(deBUG, "Attempting to unbind"); if (bound) { bound = false; unbindService(mConnection); } } private Handler mHandler = new Handler() { public voID handleMessage(Message message) { Log.d(deBUG, "Got a message!"); Bundle data = message.getData(); if (data != null) { Log.i(deBUG, "This data ain't null!"); if (data.containsKey("MESSAGE")) { mTextVIEw.setText(data.getString("MESSAGE")); } } } }; private ServiceConnection mConnection = new ServiceConnection() { public voID onServiceConnected(Componentname classname, IBinder binder) { Log.i(deBUG, "Service connected!"); outMessenger = new Messenger(binder); } @OverrIDe public voID onServicedisconnected(Componentname classname) { Log.d(deBUG, "Service disconnected!"); } };}
MyService.java
public class MyService extends Service{ private static final String deBUG = "MyService"; private Messenger inMessenger = new Messenger(new IncomingHandler()); Messenger outMessenger; private boolean bound = false; int secret = 0; @OverrIDe public voID onCreate() { Log.d(deBUG , "MyService Created"); } @OverrIDe public int onStartCommand(Intent intent, int flags, int startID) { Log.d(deBUG , "onStartCommand called"); handleCommand(intent); return START_STICKY; } Handler handler = new Handler(); public boolean running; voID handleCommand(Intent i) { Runnable r = new Runnable() { public voID run() { updateResultsInUi(); if (running) { handler.postDelayed(this, 500); } } }; r.run(); } voID updateResultsInUi() { Bundle bundle = new Bundle(); bundle.putString("MESSAGE", Integer.toString(secret = ++secret % 10)); Message message = new Message(); message.setData(bundle); if (bound) { try { outMessenger.send(message); } catch (remoteexception e) { Log.d(deBUG, "Messaging Failed"); } } else { Log.d(deBUG, "Not connected, so not messaging!"); } } @OverrIDe public IBinder onBind(Intent intent) { Log.i(deBUG, "Bound"); bound = true; Bundle extras = intent.getExtras(); outMessenger = (Messenger) extras.get("MESSENGER"); return inMessenger.getBinder(); } @OverrIDe public voID onRebind(Intent i) { bound = true; Bundle extras = i.getExtras(); outMessenger = (Messenger) extras.get("MESSENGER"); } @OverrIDe public boolean onUnbind(Intent i) { Log.d(deBUG, "onUnbind() called."); bound = false; outMessenger = null; return true; } class IncomingHandler extends Handler { @OverrIDe public voID handleMessage(Message msg) { Log.d(deBUG, "Got message"); Bundle data = msg.getData(); if (data.containsKey("ACTION")) { if ("START".equals(data.getString("ACTION"))) { if (!running) { running = true; Intent i = new Intent(getApplicationContext(), MyService.class); startService(i); } else { Log.d(deBUG, "We're already up and running!"); } } else if ("Stop".equals(data.getString("ACTION"))) { running = false; Intent i = new Intent(getApplicationContext(), MyService.class); stopService(i); } } } }}
AndroIDManifest.xml
<manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.tests.servicetest" androID:versionCode="1" androID:versionname="1.0" > <uses-sdk androID:minSdkVersion="8" androID:targetSdkVersion="15" /> <application androID:icon="@drawable/ic_launcher" androID:label="@string/app_name" androID:theme="@style/Apptheme" > <activity androID:name="com.tests.servicetest.MainActivity" androID:label="@string/Title_activity_main" androID:launchMode="singletop" > <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <service androID:name="com.tests.servicetest.MyService" androID:process=":inaneservice"> </service> </application></manifest>
对于那些想要运行它的人,activity_main.xml
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" > <TextVIEw androID:ID="@+ID/textVIEw1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerHorizontal="true" androID:layout_centerVertical="true" androID:padding="@dimen/padding_medium" androID:text="@string/hello_world" tools:context=".MainActivity" /> <button androID:ID="@+ID/bind" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignleft="@+ID/textVIEw1" androID:layout_below="@+ID/textVIEw1" androID:text="Start" /> <button androID:ID="@+ID/unbind" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignleft="@+ID/textVIEw1" androID:layout_below="@+ID/bind" androID:text="Stop" /></relativeLayout>
解决方法:
您可能要看一下:https://github.com/JonasGroeger/GPSService
总结以上是内存溢出为你收集整理的如何将我的活动绑定到我当前在Android中运行的服务?全部内容,希望文章能够帮你解决如何将我的活动绑定到我当前在Android中运行的服务?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)