Android的,我的服务无法启动 [英] Android, my service wont start

查看:180
本文介绍了Android的,我的服务无法启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图建立从每天一个服务器都有一个抓住数据的投票系统。所以,我的计划是建立AlarmManager,使其开始我的服务每天12:00和服务查找我的服务器上的一些信息,并显示基于从服务器中的数据在状态栏上的通知。

I have been trying to set up a polling system that grabs data from a server one every day. So my plan is to setup AlarmManager so that it starts my service 12:00 every day and the service looks for some info on my server and displays a notification in the status bar based on the data from the server.

问题是,我似乎无法启动,甚至我的服务以任何方式。我第一次尝试注册在AlarmManager一个的PendingIntent,但我的服务从未启动过。而现在我只是想与startService启动。

The problem is that i can't seem to even start my service in any way. I first tried to register an PendingIntent in the AlarmManager, but my service was never started. And now I'm just trying to start it with startService.

那我做错了吗?

清单:

...
<application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".skosexActivity"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.NoTitleBar"
                  android:screenOrientation="portrait">
            <service android:name=".notificationService"/>
            <receiver android:name="onBootReceiver" android:enabled="true">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
            </receiver>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter> 
</application>
...

活动:

public class homeActivity extends Activity {

    static final boolean DEBUG = true;

    static final String TAG = "HomeActivity: ";


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        mContext = getApplicationContext();

        //my try on AlarmManager

        AlarmManager mgr = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(this, notificationService.class);
        PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, 0);

        Calendar currentTime = Calendar.getInstance();
        currentTime.set(currentTime.get(Calendar.YEAR), 
                        currentTime.get(Calendar.MONTH),
                        currentTime.get(Calendar.DAY_OF_MONTH), 14, 44);

        /*mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                         SystemClock.elapsedRealtime() + 10000,
                         AlarmManager.INTERVAL_DAY, pi);*/

            //my try to just start it some how...
        Intent intent = new Intent(mContext, notificationService.class);

        Log.i("HomeActivity", "Trying to service");

        startService(new Intent(notificationService.class.getName()));
    }
...

服务code:

package se.skosex.pr.skosexp1;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class notificationService extends IntentService {

    private static final int ID = 1;

    public notificationService()
    {
        super("NotificationService");
    }

    @Override
    protected void onHandleIntent(Intent intent)
    {
        Log.i("NotificationService", "It started!");

        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.hagbard;
        CharSequence tickerText = "Kom och phesta med oss på Skövde sexmästeri kväll!";
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);

        Context context = getApplicationContext();
        CharSequence contentTitle = "Skövde Sexmästeri, test 2";
        CharSequence contentText = "Ikväll är det introdhisko och det betyder at du som nolla inte har några ursäkter att stanna hemma!";
        Intent notificationIntent = new Intent(this, eventActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);

        mNotificationManager.notify(ID, notification);
    }

}

所以,我是什么不力错了?

So, what am I dint wrong?

感谢您的帮助!

推荐答案

在Android的Manifest.xml文件注册服务

register your service in android Manifest.xml file

这篇关于Android的,我的服务无法启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆