모두. 단추 누르기 횟수를 계산하는 간단한 응용 프로그램을 만들려고합니다. 이 작업을 수행하는 백그라운드 서비스를 사용하려고합니다. 응용 프로그램은 "Press Me!"의 횟수를 계산합니다. 토스트 형태로 Exit (종료) 버튼을 클릭하면 해당 번호가 클릭되어 표시됩니다. 실행되지만 부턴 클릭 수는 표시되지 않습니다. 내 코드는 다음과 같습니다 : 나는 또한 날짜, 시간, 위도와 경도를 사용자가 응용 프로그램을 사용하는 각 시간을 확보하고자하는안드로이드 응용 프로그램에서 버튼을 몇 번 클릭했는지 측정
public class MainActivity extends Activity {
Button buttonStop, press;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonStop = (Button) findViewById(R.id.buttonStop);
press = (Button) findViewById(R.id.buttonpress);
}
public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonpress:
Toast.makeText(MainActivity.this, "Service has started", Toast.LENGTH_SHORT).show();
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
stopService(new Intent(this, MyService.class));
finish();
break;
}
}
public class MyService extends Service {
int count= 0;
MainActivity main;
/**
* @see android.app.Service#onBind(Intent)
*/
@Override
public IBinder onBind(Intent intent) {
// TODO Put your code here
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
}
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Toast.makeText(this, "Number of times this button was pressed = " + count, Toast.LENGTH_LONG).show();
}
public void onStart(Intent intent, int startid) {
count = count +1 ;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit"
android:id="@+id/buttonStop">
</Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Me!"
android:id="@+id/buttonpress">
</Button>
</LinearLayout>
. 누군가 제발 나를 도와 줄 수 있니? 이것은 처음으로 서비스를 구축하고 어떤 도움을 많이 주시면 감사하겠습니다.
호기심에서 벗어난 이유는 무엇입니까? 아니면 간단하게 서비스를 만드는 경험을 얻는 것입니까? –
언제 서비스가 생성되어야합니까? "를 누르십시오!" '버튼이나 활동 생성시? –
@Engprof : Java 코드의 버튼은 어디에 있습니까? – Mudassir