看板 AndroidDev 關於我們 聯絡資訊
最近程式需要背景執行 然後上網搜尋了關於Service的用法 網路上範例我也試過 Service.class 程式如下 public class Nickyservice extends Service{ private Handler handler = new Handler(); @Override public IBinder onBind(Intent intent){ return null; } @Override public void onCreate(){ super.onCreate(); } @Override public void onStart(Intent intent,int startId){ handler.postDelayed((Runnable) intent, startId); } @Override public void onDestroy(){ handler.removeCallbacks(showTime); super.onDestroy(); } public Runnable showTime=new Runnable(){ public void run(){ Log.i("Time:",new Date().toString()); handler.postDelayed(this, 1000); } }; } MainActivity.java 部分呼叫service則是寫 serviceButton=(ToggleButton) findViewById(R.id.serviceButton); serviceButton.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { if(isChecked){ Intent intent=new Intent(MainActivity.this,Nickyservice.class); startService(intent); }else{ Intent intent=new Intent(MainActivity.this,Nickyservice.class); stopService(intent); } } }); AndroidMenifast也加上了 <service android:name="Nickyservice" android:label="@string/app_name"> </service> 成是在執行中的時候按下ToggleButton卻直接當機跳出程式 在網路上面看到有人說要指定IBinder去綁定功能,但是在我的範例成是上面 public void onBind()卻是return null; 請問板上前輩binder的適用時機以及為什麼我的程式沒辦法順利進入背景執行 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.153.80
changyuheng:請問您的用途為何?若只要初始資料無溝通需求,用 Int 06/25 22:57
changyuheng:entService 即可 06/25 22:57
KeySabre:bindService 06/25 23:35
我在MainActivity上寫了一個手機控制程式,但因為影像程式是額外寫的,所以希望 MainActivity的控制程式可以背景執行,所以您的意思是說無須資料溝通就不必使用 IBinder的功能嗎? ※ 編輯: giginme 來自: 140.113.153.80 (06/26 00:01) ※ 編輯: giginme 來自: 140.113.153.80 (06/26 00:03)