簡體   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