Android服务无法从MainActivity启动

时间:2015-06-25 16:46:25

标签: android android-intent android-service android-intentservice

我正试图从MainService开始MainActivity。但是,我从未看到日志打印输出确认服务的创建。

我应该使用onCreateonStartCommand还是onHandleIntent?我已经尝试了所有三个,但没有任何工作。

MainActivity:

package hcii.tracker;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.aware.Aware;
import com.aware.Aware_Preferences;


public class MainActivity extends ActionBarActivity {

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

        Log.d("ACTVITY", "ACT created!");

        Context context = this;
        //MainService main = new MainService();
        //main.startService();

        //Intent i= new Intent(context, MainService.class);
        //context.startService(i);

        Intent mServiceIntent = new Intent(this, MainService.class);
        this.startService(mServiceIntent);

       //Context context = this;
        //Aware.startPlugin(context, "com.aware.plugin.google.activity_recognition");

        //startService(new Intent(this, MainService.class));


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

MainService:

package hcii.tracker;

import android.app.IntentService;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.aware.Aware;
import com.aware.Aware_Preferences;

/**s
 * Created by Connor on 6/25/2015.
 */
public class MainService extends IntentService {

    public MainService(){
        super("MainService");
    }

   /*public void onCreate(){

        Log.d("SERVICE", "Service created!");

        Context context = this;
        //Activate Accelerometer
        Aware.setSetting(this, Aware_Preferences.STATUS_ACCELEROMETER, true);
        //Set sampling frequency
        Aware.setSetting(this, Aware_Preferences.FREQUENCY_ACCELEROMETER, 200000);
        //Apply settings
        sendBroadcast(new Intent(Aware.ACTION_AWARE_REFRESH));
        //Aware.startPlugin(getApplicationContext(), "com.aware.plugin.google.activity_recognition");
        Aware.startPlugin(context, "com.aware.plugin.google.activity_recognition");
    }*/

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("SERVICE", "Handle");

        Context context = this;
        //Activate Accelerometer
        Aware.setSetting(this, Aware_Preferences.STATUS_ACCELEROMETER, true);
        //Set sampling frequency
        Aware.setSetting(this, Aware_Preferences.FREQUENCY_ACCELEROMETER, 200000);
        //Apply settings
        sendBroadcast(new Intent(Aware.ACTION_AWARE_REFRESH));
        //Aware.startPlugin(getApplicationContext(), "com.aware.plugin.google.activity_recognition");
        Aware.startPlugin(context, "com.aware.plugin.google.activity_recognition");
    }

    /*@Override
    public int onStartCommand(Intent intent, int i, int u) {
        Log.d("SERVICE", "Start command");

        Context context = this;
        //Activate Accelerometer
        Aware.setSetting(this, Aware_Preferences.STATUS_ACCELEROMETER, true);
        //Set sampling frequency
        Aware.setSetting(this, Aware_Preferences.FREQUENCY_ACCELEROMETER, 200000);
        //Apply settings
        sendBroadcast(new Intent(Aware.ACTION_AWARE_REFRESH));
        //Aware.startPlugin(getApplicationContext(), "com.aware.plugin.google.activity_recognition");
        Aware.startPlugin(context, "com.aware.plugin.google.activity_recognition");
        return 0;
    }*/
}

清单文件:

<?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="hcii.tracker" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        tools:replace="android:icon, android:theme">
        <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>
        <Service
            android:name=".MainService"
            android:exported="false"/>

        <provider
            android:name="com.aware.providers.Bluetooth_Provider"
            android:authorities="${applicationId}.provider.bluetooth"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Applications_Provider"
            android:authorities="${applicationId}.provider.applications"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Locations_Provider"
            android:authorities="${applicationId}.provider.locations"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Installations_Provider"
            android:authorities="${applicationId}.provider.installations"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.ESM_Provider"
            android:authorities="${applicationId}.provider.esm"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Temperature_Provider"
            android:authorities="${applicationId}.provider.temperature"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Linear_Accelerometer_Provider"
            android:authorities="${applicationId}.provider.accelerometer.linear"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Gravity_Provider"
            android:authorities="${applicationId}.provider.gravity"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Barometer_Provider"
            android:authorities="${applicationId}.provider.barometer"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Magnetometer_Provider"
            android:authorities="${applicationId}.provider.magnetometer"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Proximity_Provider"
            android:authorities="${applicationId}.provider.proximity"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Light_Provider"
            android:authorities="${applicationId}.provider.light"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Telephony_Provider"
            android:authorities="${applicationId}.provider.telephony"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Aware_Provider"
            android:authorities="${applicationId}.provider.aware"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Rotation_Provider"
            android:authorities="${applicationId}.provider.rotation"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.WiFi_Provider"
            android:authorities="${applicationId}.provider.wifi"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Accelerometer_Provider"
            android:authorities="${applicationId}.provider.accelerometer"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Gyroscope_Provider"
            android:authorities="${applicationId}.provider.gyroscope"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Traffic_Provider"
            android:authorities="${applicationId}.provider.traffic"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Mqtt_Provider"
            android:authorities="${applicationId}.provider.mqtt"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Processor_Provider"
            android:authorities="${applicationId}.provider.processor"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.TimeZone_Provider"
            android:authorities="${applicationId}.provider.timezone"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Battery_Provider"
            android:authorities="${applicationId}.provider.battery"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Screen_Provider"
            android:authorities="${applicationId}.provider.screen"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Network_Provider"
            android:authorities="${applicationId}.provider.network"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Communication_Provider"
            android:authorities="${applicationId}.provider.communication"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
        <provider
            android:name="com.aware.providers.Keyboard_Provider"
            android:authorities="${applicationId}.provider.keyboard"
            android:exported="true"
            android:readPermission="com.aware.READ_CONTEXT_DATA"
            android:writePermission="com.aware.WRITE_CONTEXT_DATA"
            >
        </provider>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

背景服务指南可在https://developer.android.com/training/run-background-service/create-service.html找到。

您将在主要活动中使用startService方法,这将调用onHandleIntent

看起来你的方式是正确的。但是,在您的清单<service...中应该是小写的。