开发者

Android, my service wont start

开发者 https://www.devze.com 2023-03-29 17:39 出处:网络
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 so

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.

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.

So what am I doing wrong?

Manifest:

...
<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>
...

Activity:

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()));
    }
...

Service 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?

Thanks for any help!


register your service in android Manifest.xml file


Probably it's your call to startService try something like this instead:

   Intent intent = new Intent(mContext, notificationService.class);
   startService(intent)

It looks like your call to startService() has some test code in it?


Hazarding a guess...perhaps you are missing a needed permission?

http://developer.android.com/reference/android/Manifest.permission.html

String  SET_ALARM   Allows an application to broadcast an Intent to set an alarm for the user.


if your trying to access a server... you need to add

<uses-permission android:name="android.permission.INTERNET"/>

to the manifest or most likely the intent will fail silently

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号