java - 无法从不同包中的 Activity 启动 Android 服务

标签 java android android-service

我是 Android 堆栈新手。我正在尝试从启动器 Activity 启动android服务。 ServiceActivity 在单独的包中定义,但尚未启动。在logcat中没有异常或错误。我在 stackoverflow 上检查了有关此问题的许多问题,但没有成功。以下是我的应用程序的源代码。我在这个问题上花了将近8个小时。任何帮助将不胜感激。

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="nl.test.app">

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:resizeable="true"
        android:smallScreens="true"
        android:xlargeScreens="true" />

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">           
        <activity
            android:name=".ui.LoginActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>    
        <service
            android:name=".messaging.AlertService"
            android:enabled="true"
            android:exported="true">
        </service>
    </application>   
</manifest>

AlertService.java:

package nl.test.app.messaging;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class AlertService extends Service {

    public AlertService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(getApplicationContext(), "on create called\n", Toast.LENGTH_LONG).show();

    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }
}

LoginActivity.java

package nl.test.app.ui;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import nl.test.app.R;

public class LoginActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

    }

    // function for service testing
    public void onStartButtonClick(View view) {
        Intent myIntentToStartAlertListActivity = new Intent();
        String pkg = "nl.test.app.messaging";
        String cls = "nl.test.app.messaging.AlertService";
        myIntentToStartAlertListActivity.setComponent(new ComponentName(pkg, cls));

     if (startService(myIntentToStartAlertListActivity) != null) {
            Log.i("Service Started","Service started");
            Toast.makeText(getApplicationContext(), "Service is running\n", Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(getApplicationContext(), "Service is not running\n", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();

    }
}

最佳答案

试试这个

public void onStartButtonClick(View view) {
    Intent myIntentToStartAlertListActivity = new Intent(LoginActivity.this, AlertService.class);
 if (startService(myIntentToStartAlertListActivity) != null) {
        Log.i("Service Started","Service started");
        Toast.makeText(getApplicationContext(), "Service is running\n", Toast.LENGTH_LONG).show();
    }
    else {
        Toast.makeText(getApplicationContext(), "Service is not running\n", Toast.LENGTH_LONG).show();
    }
}

关于java - 无法从不同包中的 Activity 启动 Android 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41571393/

相关文章:

java - 将文件放置在文件系统中应触发 java 应用程序

java - 什么是NullPointerException,我该如何解决?

android - 无法动态更改自定义 DialogFragment 布局

java - android ListView requestFocusFromTouch setSelection smoothScrollToPosition 似乎被忽略了

java - Android:如何告诉我的 Activity 从我的服务启动新 Activity ?

java - NetBeans IDE - ClassNotFoundException : net. ucanaccess.jdbc.UcanaccessDriver

java - 使用 selenium 将内容拖放到文本框中

java - 创建菜单但两个按钮之一无法正常工作

android - 我的服务没有被破坏 - 如何停止服务

android - 在不同的进程中将自定义对象传递给 android 服务