0

我不知道为什么,但每次杀死我的应用程序时,通知也会被删除并且没有创建服务。它也应该在启动时启动,但似乎没有..

我的清单:

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="Test"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".auth.Splash"
        android:label="Ruby" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <!--
             android:enabled="true"
        android:exported="false"
    -->
    <receiver android:name=".service.AutoStart" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

    <service
        android:name="com.octo.android.robospice.JacksonSpringAndroidSpiceService"
        android:exported="false" />
</application>

自动启动.java

package com.test.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class AutoStart extends BroadcastReceiver {
    public void onReceive(Context arg0, Intent arg1) {
        Intent intent = new Intent(arg0, SimpleService.class);
        arg0.startService(intent);
        Log.i("Autostart", "started");
    }
}

简单服务.java

    package com.test.service;

import com.test.R;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.TextView;
import android.widget.Toast;

public class SimpleService extends Service {
    public class NotifyMessage extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            TextView txt = new TextView(this);

            txt.setText("Activity after click on notification");
            setContentView(txt);
        }
    }

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

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Service created", Toast.LENGTH_LONG).show();
        final NotificationManager mgr = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification note = new Notification(
                R.drawable.abc_ab_bottom_transparent_dark_holo,
                "Android Example Status message!", System.currentTimeMillis());

        // This pending intent will open after notification click
        PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(this,
                NotifyMessage.class), 0);

        note.setLatestEventInfo(this, "Android Example Notification Title",
                "This is the android example notification message", i);

        // After uncomment this line you will see number of notification arrived
        note.number = 2;
        mgr.notify(3, note);
    }

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

    @Override
    public void onStart(Intent intent, int startId) {
        super.onCreate();
        Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO do something useful
        return Service.START_NOT_STICKY;
    }
}

飞溅.java

package com.test.auth;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;

import com.test.R;
import com.test.mobile.MainActivity;
import com.test.service.SimpleService;

public class Splash extends Activity {

    // Session Manager Class
    SessionManager session;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set View to register.xml
        setContentView(R.layout.splash_activity);
        startService(new Intent(this, SimpleService.class));

    }
}
4

1 回答 1

0

服务依赖于应用程序实例。如果你杀死你的应用程序,你也会杀死你的服务。如果您想运行服务,即使应用程序被杀死,您也需要通过startForeground(). BOOT 标志也不用于“启动应用程序”。在这种情况下,意味着 Android OS 启动。

于 2014-10-21T18:28:27.830 回答