gpt4 book ai didi

android - Sinch Service 好像没有启动 Android Studio 和 Parse.com (Android Messaging Tutorial)

转载 作者:行者123 更新时间:2023-11-29 01:34:14 29 4
gpt4 key购买 nike

我正在学习本教程:https://www.sinch.com/tutorials/android-messaging-tutorial-using-sinch-and-parse

Github 教程:https://github.com/sinch/android-messaging-tutorial

我遇到的问题是该服务似乎根本没有启动。我已经检查了代码,但没有发现任何看起来不对的地方(但话又说回来,我是初学者,哈哈)。

我在 MenuFragment3 类中没有收到任何广播,因此 progressDialog 一直在旋转。我还检查了一项服务是否同时在多个模拟器和真实设备上运行,就像我说的那样,服务似乎永远不会启动,我真的不明白为什么。

如果您需要更多信息,请告诉我,感谢您的帮助!

首先我在用户登录时启动服务:

serviceIntent = new Intent(getApplicationContext(), MessageService.class);
startService(serviceIntent);

这是 list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yupo.dominic.yupo" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<activity
android:name=".UserAuth"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
android:label="@string/title_login_area">
</activity>
<activity
android:name=".StudentLogged"
android:label="@string/title_student_area">
</activity>
<activity
android:name=".Register"
android:label="@string/register">
</activity>
<activity
android:name=".LectureLogged"
android:label="@string/title_lecture_area">
</activity>
<service android:name=".MessageService">

</service>
</application>

</manifest>

这是 MessageService 类:

package com.yupo.dominic.yupo;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;

import com.parse.Parse;
import com.parse.ParseUser;
import com.sinch.android.rtc.ClientRegistration;
import com.sinch.android.rtc.Sinch;
import com.sinch.android.rtc.SinchClient;
import com.sinch.android.rtc.SinchClientListener;
import com.sinch.android.rtc.SinchError;
import com.sinch.android.rtc.messaging.MessageClient;
import com.sinch.android.rtc.messaging.MessageClientListener;
import com.sinch.android.rtc.messaging.WritableMessage;

public class MessageService extends Service implements SinchClientListener {

private static final String APP_KEY = "mykeyiscorrect";
private static final String APP_SECRET = "mysecretiscorrect";
private static final String ENVIRONMENT = "sandbox.sinch.com";
private final MessageServiceInterface serviceInterface = new MessageServiceInterface();
private SinchClient sinchClient = null;
private MessageClient messageClient = null;
private String currentUserId;
private LocalBroadcastManager broadcaster;
private Intent broadcastIntent = new Intent("com.yupo.dominic.yupo.StudentLogged");

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

Parse.initialize(this, "thisiscorrect", "thisiscorrect");

currentUserId = ParseUser.getCurrentUser().getObjectId();

if (currentUserId != null && !isSinchClientStarted()) {
startSinchClient(currentUserId);
}

broadcaster = LocalBroadcastManager.getInstance(this);

return super.onStartCommand(intent, flags, startId);
}

public void startSinchClient(String username) {
sinchClient = Sinch.getSinchClientBuilder().context(this).userId(username).applicationKey(APP_KEY)
.applicationSecret(APP_SECRET).environmentHost(ENVIRONMENT).build();

sinchClient.addSinchClientListener(this);

sinchClient.setSupportMessaging(true);
sinchClient.setSupportActiveConnectionInBackground(true);

sinchClient.checkManifest();
sinchClient.start();
}

private boolean isSinchClientStarted() {
return sinchClient != null && sinchClient.isStarted();
}

@Override
public void onClientFailed(SinchClient client, SinchError error) {
broadcastIntent.putExtra("success", false);
broadcaster.sendBroadcast(broadcastIntent);

sinchClient = null;
}

@Override
public void onClientStarted(SinchClient client) {
broadcastIntent.putExtra("success", true);
broadcaster.sendBroadcast(broadcastIntent);

client.startListeningOnActiveConnection();
messageClient = client.getMessageClient();
}

@Override
public void onClientStopped(SinchClient client) {
sinchClient = null;
}

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

@Override
public void onLogMessage(int level, String area, String message) {
}

@Override
public void onRegistrationCredentialsRequired(SinchClient client, ClientRegistration clientRegistration) {
}

public void sendMessage(String recipientUserId, String textBody) {
if (messageClient != null) {
WritableMessage message = new WritableMessage(recipientUserId, textBody);
messageClient.send(message);
}
}

public void addMessageClientListener(MessageClientListener listener) {
if (messageClient != null) {
messageClient.addMessageClientListener(listener);
}
}

public void removeMessageClientListener(MessageClientListener listener) {
if (messageClient != null) {
messageClient.removeMessageClientListener(listener);
}
}

@Override
public void onDestroy() {
sinchClient.stopListeningOnActiveConnection();
sinchClient.terminate();
}

public class MessageServiceInterface extends Binder {
public void sendMessage(String recipientUserId, String textBody) {
MessageService.this.sendMessage(recipientUserId, textBody);
}

public void addMessageClientListener(MessageClientListener listener) {
MessageService.this.addMessageClientListener(listener);
}

public void removeMessageClientListener(MessageClientListener listener) {
MessageService.this.removeMessageClientListener(listener);
}

public boolean isSinchClientStarted() {
return MessageService.this.isSinchClientStarted();
}
}
}

这里是教程中我的 ListUsersActivity 版本(对我来说,它称为 MenuFragment3,它是从 StudentLogged 或 LectureLogged Activity 调用的滑动菜单的一部分)

package com.yupo.dominic.yupo;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.ParseQuery;
import com.parse.ParseUser;

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

public class MenuFragment3 extends Fragment{

View rootview;
ArrayList<String> names;
ParseUser currentUser;
String currentUserId;
ListView usersListView;
ArrayAdapter<String> namesArrayAdapter;
ProgressDialog progressDialog;
BroadcastReceiver receiver;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

currentUser = ParseUser.getCurrentUser();

if(currentUser.getString("type").equalsIgnoreCase("Lecturer"))
{
rootview = inflater.inflate(R.layout.menu3_2_layout, container, false);
}
else
{
rootview = inflater.inflate(R.layout.menu3_layout, container, false);
}

progressDialog = new ProgressDialog(getActivity());
progressDialog.setTitle("Loading");
progressDialog.setMessage("Please wait...");
progressDialog.show();

receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getActivity(), "broadcast received", Toast.LENGTH_SHORT).show();

Boolean success = intent.getBooleanExtra("success", false);
progressDialog.dismiss();
//show a toast message if the Sinch
//service failed to start
if (!success) {
Toast.makeText(getActivity(), "Messaging service failed to start", Toast.LENGTH_LONG).show();
}
}
};
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, new IntentFilter("com.yupo.dominic.yupo.StudentLogged"));


currentUserId = ParseUser.getCurrentUser().getObjectId();
names = new ArrayList<String>();
ParseQuery<ParseUser> query = ParseUser.getQuery();

query.whereNotEqualTo("objectId", currentUserId);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> userList, com.parse.ParseException e) {
if (e == null) {
for (int i=0; i<userList.size(); i++) {
names.add(userList.get(i).getUsername().toString());
}
usersListView = (ListView)rootview.findViewById(R.id.usersListView);
namesArrayAdapter =
new ArrayAdapter<String>(getActivity(),
R.layout.user_list_item, names);
usersListView.setAdapter(namesArrayAdapter);
usersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
openConversation(names, i);
}
});
} else {
Toast.makeText(getActivity(),
"Error loading user list",
Toast.LENGTH_LONG).show();
}
}
});

return rootview;
}

public void openConversation(ArrayList<String> names, int pos) {
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", names.get(pos));
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> user, com.parse.ParseException e) {
if (e == null) {
//Intent intent = new Intent(getActivity(), MessagingActivity.class);
//intent.putExtra("RECIPIENT_ID", user.get(0).getObjectId());
//startActivity(intent);

Toast.makeText(getActivity(),
"working",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(),
"Error finding that user",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

最佳答案

您正在尝试使用您的 Sinch key 和 secret 来初始化 Parse。此外,您应该在应用程序的 onCreate 方法中初始化 Parse。尝试:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

currentUserId = ParseUser.getCurrentUser().getObjectId();

if (currentUserId != null && !isSinchClientStarted()) {
startSinchClient(currentUserId);
}

broadcaster = LocalBroadcastManager.getInstance(this);

return super.onStartCommand(intent, flags, startId);
}

关于android - Sinch Service 好像没有启动 Android Studio 和 Parse.com (Android Messaging Tutorial),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29482754/

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