测量多少次按钮被点击的Andr​​oid应用程序多少次、应用程序、测量、按钮

2023-09-07 12:39:49 作者:相逢何必曾相识

大家。我试图创建一个简单的应用程序,计数的次数一个按钮pressed。我试图用一个后台服务做到这一点。该应用程序应该算多少次preSS我!被点击并显示数字点击举杯形式的退出按钮时。它运行,但它并没有显示buuton的点击次数。我的code是如下:

everyone. I am trying to create a simple application that counts the number of times a button is pressed. I am trying to use a background service to do this. The application is supposed to count how many times the "Press Me!" was clicked and display that number when the Exit button is clicked in the form of a toast. It runs, but it does not show the number of buuton clicks. My code is below:

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>

我也想在用户使用应用程序的每个时间获得的日期,时间,纬度和经度。可能有人帮助我吗?这是我第一次构建一个服务和任何帮助将非常AP preciated。

I would also like to obtain the date, time, latitude and longitude each time the user uses the application. Could somebody help me please? This is my first time building a service and any help would be much appreciated.

推荐答案

您几乎没有!

三件事情忘了做:

让你的MainActivity实施 View.OnClickListener - 如果你已经使用 @覆盖您的onClick()方法之前,编译器会一直放倒你。一般情况下,当你实现您所期望的框架来调用,使用 @覆盖作为完整性检查功能。

Have your MainActivity implement View.OnClickListener - If you had used @Override before your onClick() method the compiler would've tipped you off. In general, whenever you implement a function that you expect the framework to call, use @Override as a sanity check.

附上您的MainActivity使用你的按钮 View.setOnClickListener() - 这实际上告诉按钮来调用你的onClick()方法被点击时,

Attach your MainActivity to your buttons using View.setOnClickListener() - this actually tells the buttons to call your onClick() method when they are clicked.

(可能)的在您的Andr​​oidManifest.xml 定义服务 - 棒&LT;服务机器人:名字=则将MyService/&GT; 的元素中。如果你不这样做,你可能已经通过查看logcat中(DDMS或Debug透视图)发现了。它会一直说:

(possibly) Define your service in your AndroidManifest.xml - Stick <service android:name="MyService"/> inside your element. If you didn't do this, you could've found out by looking in Logcat (DDMS or Debug perspective). It would've said:

无法启动服务的意图[...]:没有找到

Unable to start service intent [...]: not found

这就是我在你的code键更改,然后祝酒词出现了我的屏幕与相应的点击次数上。

That's all I had to change in your code, and then the toasts showed up on my screen with the appropriate click count.