gpt4 book ai didi

Android,我的服务无法启动

转载 作者:行者123 更新时间:2023-11-29 22:21:18 26 4
gpt4 key购买 nike

我一直在尝试建立一个每天从服务器获取数据的轮询系统。因此,我的计划是设置 AlarmManager,使其每天 12:00 启动我的服务,该服务会在我的服务器上查找一些信息,并根据来自服务器的数据在状态栏中显示通知。

问题是我什至无法以任何方式启动我的服务。我首先尝试在 AlarmManager 中注册一个 PendingIntent,但我的服务从未启动过。现在我只是想用 startService 启动它。

那我做错了什么?

list :

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

服务代码:

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

}

那么,我哪里错了?

感谢您的帮助!

最佳答案

在android Manifest.xml 文件中注册你的服务

关于Android,我的服务无法启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7115317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com