gpt4 book ai didi

android - 由服务扩展的类 Activity 问题

转载 作者:行者123 更新时间:2023-11-30 05:09:07 25 4
gpt4 key购买 nike

我有这段代码,如果一段时间之前分数不同,我会在其中发出通知。但是不要使用这段代码,因为 Android Studio 告诉我:

1) 无法解析构造函数 Intent(anonymous.retrofit2.Callback, java.lang.Class)

2)

getActivity() 或变量 this 在带有参数的代码中不起作用:Cannot resolve method getActivity()

`

package com.example.luky.nhlvysledky;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.luky.nhlvysledky.api_data.ApiTools;
import com.example.luky.nhlvysledky.api_data.LastMatchModel;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class GameChangeService extends Service {
private final static String TAG = "GameChangeService";
private BroadcastReceiver broadcastReceiver;
private Handler h;
private SharedPreferences sp;
Notification notif;
NotificationManager notifManager;
private SharedPreferences.Editor ed;
private Runnable r;
private int id;

private String CHANNEL_ID = "ID";
private int notifId = 1000;


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

@Override
public void onCreate() {
super.onCreate();
sp = getSharedPreferences(Tools.PACKAGE_NAME, Context.MODE_PRIVATE);
ed = sp.edit();
notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
h = new Handler();
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
stopSelf();
}
};
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, new IntentFilter(Tools.INTENT_ACTION_STOP_SERVICE));
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
id = intent.getIntExtra(Tools.INTENT_EXTRA_ID, -1);
if (id == -1) {
stopSelf();
} else {
r = new Runnable() {
@Override
public void run() {
doStuff(id);
h.postDelayed(r, 15000);
}
};
h.post(r);

return Service.START_STICKY;
}
//TOTO TU JE VELMI DISKUTABILNE
return Service.START_STICKY;
}

private void doStuff(final int id) {
//TODO: Checkni pls ci je boxscore updatovany live alebo nie. Ak je tak ho mozes pouzit v IApiDefinition namiesto live feed
//TODO: JA> V schedule je s gamepk aj online zapas s golmi - staci to pouzit
ApiTools.getApi().getGame(id).enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (sp.contains(Integer.toString(id))) {
int povodnyPocetGolovVZapase = sp.getInt(Integer.toString(id), 0);

//Z responsu zistit kolko eventov je teraz v zapase, t.j. ci uz zapas zacal.
//Dalej zistit ci su tam nejake eventy, ktore maju typ goal alebo ENDGAME (asi).
//AK sa zmenil pocet golov, tak posli notifikaciu ze padol gol aj s novym stavom

JsonObject data = response.body();
//pouzijem lastmatchmodel, aj ked to nie je pre toto robene, ale data mi stacia aj z neho
//List<LastMatchModel> livezapasy = new ArrayList<>();
//vytiahnem si zoznam
JsonArray zoznamZapasovZJsonu = data.get("dates").getAsJsonArray();
for (int i = 0; i < zoznamZapasovZJsonu.size(); i++) {
//pouzijem lastmatchmodel, aj ked to nie je pre toto robene, ale data mi stacia aj z neho
LastMatchModel novyZapas = new LastMatchModel();
JsonObject zapasDate = zoznamZapasovZJsonu.get(i).getAsJsonObject();

JsonArray games = zapasDate.get("games").getAsJsonArray();
if (games.size() == 1) {
JsonObject teams = games.get(0).getAsJsonObject().get("teams").getAsJsonObject();

int golyHostia = teams.get("away").getAsJsonObject().get("score").getAsInt();
String timHostia = teams.get("away").getAsJsonObject().get("team").getAsJsonObject().get("name").getAsString();

int golyDomaci = teams.get("home").getAsJsonObject().get("score").getAsInt();
String timDomaci = teams.get("home").getAsJsonObject().get("team").getAsJsonObject().get("name").getAsString();

if (golyDomaci + golyHostia != povodnyPocetGolovVZapase) {

/*HERE IS THIS CODE TO REPAIR*/
Intent intent = new Intent(this, MatchNotification.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
createNotificationChannel();


final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getActivity(), CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("GOAL")
.setContentText(timDomaci + golyDomaci + " vs " + golyHostia + timHostia)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);

NotificationManagerCompat nm = NotificationManagerCompat.from(getActivity());
nm.notify(notifId, mBuilder.build());

}
}
}
/*HERE IS ENDING CODE*/

} else {
int povodnyPocetEventovVZapase = 0;
}
}

@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
Log.e(GameChangeService.TAG, "Nebavi to ");
}
});
}

@Override
public void onDestroy() {
super.onDestroy();
if (h != null)
h.removeCallbacks(r);
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
}

private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Name of the channel";
String description = "Description of the channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
`

你能帮我解决这个问题吗?在 fragments 中可以使用 getActivity 或 getContext,但是这个类不是通过 Fragment 扩展的,而是通过 Service 扩展的。谢谢!

最佳答案

Intent intent = new Intent(this, MatchNotification.class);行你在匿名的方法new Callback<JsonObject>()对象。

意思是thisCallback<JsonObject>此时。

要修复它,您必须指定要使用的“this”。作为Intent需要 Context ,你要找的就是你的Service对象(因为Service在Android中是Context)

Intent intent = new Intent(GameChangeService.this, MatchNotification.class);例如。

关于android - 由服务扩展的类 Activity 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54011190/

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