繁体   English   中英

按下按钮应该可以启动服务并显示文本,但是当我单击按钮时什么也没有发生

[英]Pressing a button is supposed to start service and display text, but nothing happens when I click the button

我是Java的新手,我试图制作一个简单的程序来使用服务按下按钮时显示文本。 由于某些原因,当我按下“启动服务”和“停止服务”按钮时,什么也没有发生。 这是我的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void startService(View view) {
    startService(new Intent(getBaseContext(), MyService.class));
}
public void stopService(View view) {
    stopService(new Intent(getBaseContext(), MyService.class));
}

public static class MyService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    }
}

和按钮-

<Button
    android:id="@+id/button"
    android:layout_width="132dp"
    android:layout_height="105dp"
    android:layout_marginTop="8dp"
    android:onClick="startService"
    android:text="@string/Buttton1"
    android:visibility="visible"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button2"
    android:layout_width="128dp"
    android:layout_height="108dp"
    android:layout_marginTop="8dp"
    android:onClick="stopService"
    android:text="@string/Button2"
    android:visibility="visible"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button" />

我正在使用一个片段,这就是为什么它不起作用的原因。 我使用了MainActivity,而且效果很好。 感谢您的回答!

一些错误的地方。
1:我认为您的服务不应该是静态的。
2:您是否在AndroidManifes.xml中声明了服务?
3:不需要使用getBaseContext()

  • 如果您正在参加活动,请使用此功能
  • 如果您在片段中,请使用getActivity()

因此,您必须在android清单中声明所有服务与所有活动相同。为此,导航到位于app> manifest> AndroidManifext.xml下的应用清单,接下来,您需要在application下添加一个名称为service的服务标签您的服务。 示例代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.android.myapplication">

    <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <service android:name=".MyService"/> <-----This is were you declared your service
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

在那之后一切都应该正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM