gpt4 book ai didi

android - 将 Twilio 的传入调用代码从 Activity 实现到服务类中,但每次都会出错

转载 作者:行者123 更新时间:2023-11-30 02:01:32 27 4
gpt4 key购买 nike

在这里,我试图为来电显示一个Dialog,当我在一个 Activity 中实现时它工作正常,但我想在后台服务中实现,这样我就可以随时接到电话电话。我将 Service 用于后台进程,但由于某些方法,我没有获得适当的成功。当我在 Service 中实现时,我总是在同一行出错,但在 Activity 中永远不会出错。

我从 BROADCASTRECEIVER 开始这个服务

public class BReciever extends BroadcastReceiver {
Context context;

@Override
public void onReceive(Context con, Intent intent) {
this.context = con;
intent = new Intent(context, PingService.class);
context.startService(intent);
intent = new Intent(context, IncomingCallService.class);
intent.putExtra("operator_id", "12");
context.startService(intent);

}

}

我得到错误的方法是

  public boolean handleIncomingIntent(Intent intent) {
Device inDevice = intent.getParcelableExtra(Device.EXTRA_DEVICE);
Connection inConnection = intent
.getParcelableExtra(Device.EXTRA_CONNECTION);
if (inDevice == null && inConnection == null)
return false;

intent.removeExtra(Device.EXTRA_DEVICE);
intent.removeExtra(Device.EXTRA_CONNECTION);

if (pendingIncomingConnection != null) {
Log.i(TAG, "A pending connection already exists");
inConnection.ignore();
return false;
}

pendingIncomingConnection = inConnection;
pendingIncomingConnection.setConnectionListener(this);

return true;
}

当我通过 Service 调用此方法时,我总是在 DeviceConnection 中得到 null。

服务等级

public class IncomingCallService extends Service implements LoginListener,
BasicConnectionListener, BasicDeviceListener, View.OnClickListener,
CompoundButton.OnCheckedChangeListener,
RadioGroup.OnCheckedChangeListener {

private static Handler handler;
public BasicPhone phone;
BasicPhoneActivity call_screen;
SharedPreferences login_details;
Vibrator vibrator;
Ringtone r;
private AlertDialog incomingAlert;
Uri notification;
public static final String LOGIN_DETAILS = "u18_sales_logins_details";
AudioManager am;
Intent intent;
static String Twilio_id = "",
INCOMING_AUTH_PHP_SCRIPT = "http://example.com/client_generate_token?users_id=";
Runnable newrun;
Activity context;
Context ctx;

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
login_details = getSharedPreferences(LOGIN_DETAILS,
Context.MODE_PRIVATE);
if (login_details.contains("twilio_Id")) {
Twilio_id = login_details.getString("twilio_Id", "");
}
phone = BasicPhone.getInstance(getApplicationContext());
phone.setListeners(this, this, this);
new IncomingTokenTask().execute();
onCallHandler();
Log.d("toekn", BasicPhone.capabilityToken);
}

@Override
public void onDestroy() {
super.onDestroy();
if (phone != null) {
phone.setListeners(null, null, null);
phone = null;
}
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
op_id = (String) intent.getExtras().get("operator_id");
if (null != intent) {
if (phone.handleIncomingIntent(intent)) {
showIncomingAlert();
}
}
return START_STICKY;
}

private void showIncomingAlert() {

handler.post(new Runnable() {
@Override
public void run() {
if (incomingAlert == null) {
notification = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
r = RingtoneManager.getRingtone(getApplicationContext(),
notification);
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
r.play();
break;
case AudioManager.RINGER_MODE_VIBRATE:
long pattern[] = { 0, 500, 200, 300, 500 };
vibrator.vibrate(pattern, 0);
break;
case AudioManager.RINGER_MODE_NORMAL:
r.play();
break;
}
incomingAlert = new AlertDialog.Builder(
getApplicationContext())
.setTitle(R.string.incoming_call)
.setCancelable(false)
.setMessage(R.string.incoming_call_message)
.setPositiveButton(R.string.answer,
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
r.stop();
break;
case AudioManager.RINGER_MODE_VIBRATE:
vibrator.cancel();
break;
case AudioManager.RINGER_MODE_NORMAL:
r.stop();
break;
}
phone.acceptConnection();
incomingAlert = null;
}
})
.setNegativeButton(R.string.ignore,
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
r.stop();
break;
case AudioManager.RINGER_MODE_VIBRATE:
vibrator.cancel();
break;
case AudioManager.RINGER_MODE_NORMAL:
r.stop();
break;
}
phone.ignoreIncomingConnection();
incomingAlert = null;
}
})
.setOnCancelListener(
new DialogInterface.OnCancelListener() {
@Override
public void onCancel(
DialogInterface dialog) {
phone.ignoreIncomingConnection();
}
}).create();
incomingAlert.show();
}
}
});
}

private void hideIncomingAlert() {
handler.post(new Runnable() {
@Override
public void run() {

if (incomingAlert != null) {
incomingAlert.dismiss();
incomingAlert = null;
}
}
});
}

public void onCallHandler() {

// TODO Auto-generated method stub

handler = new Handler();
newrun = new Runnable() {

@Override
public void run() {
handler.removeCallbacks(newrun);
new IncomingTokenTask().execute();
if (null != intent) {
if (phone.handleIncomingIntent(intent)) {
showIncomingAlert();
}
}
handler.postDelayed(this, 200000);
}
};
handler.postDelayed(newrun, 80000);

}

class IncomingTokenTask extends AsyncTask<Void, Void, Void> {
String message;
JSONObject jsonResponse;
int crash_app;

@Override
protected void onPreExecute() {
}

@Override
protected Void doInBackground(Void... params) {
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
DefaultHttpClient httpclient = new DefaultHttpClient();
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory
.getSocketFactory();
socketFactory
.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
SingleClientConnManager mgr = new SingleClientConnManager(
httpclient.getParams(), registry);
@SuppressWarnings("unused")
DefaultHttpClient httpClient = new DefaultHttpClient(mgr,
httpclient.getParams());
// Set verifier
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);// Create
// new
// HTTP
// POST
// with
// URL
// to
// php
// file
// as
// parameter

HttpGet httppost = new HttpGet(INCOMING_AUTH_PHP_SCRIPT + Twilio_id);
try {
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
if (entity != null) {
BasicPhone.capabilityToken = EntityUtils
.toString(entity);
BasicPhone.decodedString = BasicPhone.capabilityToken
.replace("\"", "");
}
}
} catch (Exception e) {
crash_app = 5;
message = "Something went wrong. Please try again later.";
return null;
}
return null;
}

@Override
protected void onPostExecute(Void result) {
}
}

@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub

}

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub

}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

}

@Override
public void onDeviceStartedListening() {
// TODO Auto-generated method stub

}

@Override
public void onDeviceStoppedListening(Exception error) {
// TODO Auto-generated method stub

}

@Override
public void onIncomingConnectionDisconnected() {
hideIncomingAlert();
}

@Override
public void onConnectionConnecting() {
// TODO Auto-generated method stub

}

@Override
public void onConnectionConnected() {
// TODO Auto-generated method stub

}

@Override
public void onConnectionFailedConnecting(Exception error) {
// TODO Auto-generated method stub

}

@Override
public void onConnectionDisconnecting() {
// TODO Auto-generated method stub

}

@Override
public void onConnectionDisconnected() {
// TODO Auto-generated method stub

}

@Override
public void onConnectionFailed(Exception error) {
// TODO Auto-generated method stub

}

@Override
public void onLoginStarted() {
// TODO Auto-generated method stub

}

@Override
public void onLoginFinished() {
// TODO Auto-generated method stub

}

@Override
public void onLoginError(Exception error) {
// TODO Auto-generated method stub

}

}

而 logcat 是

07-16 00:12:56.305: E/AndroidRuntime(1104): FATAL EXCEPTION: main
07-16 00:12:56.305: E/AndroidRuntime(1104): Process: com.u18.enroll, PID: 1104
07-16 00:12:56.305: E/AndroidRuntime(1104): java.lang.NullPointerException
07-16 00:12:56.305: E/AndroidRuntime(1104): at com.u18.enroll.BasicPhone.handleIncomingIntent(BasicPhone.java:290)
07-16 00:12:56.305: E/AndroidRuntime(1104): at com.u18.enroll.IncomingCallService$3.run(IncomingCallService.java:217)
07-16 00:12:56.305: E/AndroidRuntime(1104): at android.os.Handler.handleCallback(Handler.java:733)
07-16 00:12:56.305: E/AndroidRuntime(1104): at android.os.Handler.dispatchMessage(Handler.java:95)
07-16 00:12:56.305: E/AndroidRuntime(1104): at android.os.Looper.loop(Looper.java:136)
07-16 00:12:56.305: E/AndroidRuntime(1104): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-16 00:12:56.305: E/AndroidRuntime(1104): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 00:12:56.305: E/AndroidRuntime(1104): at java.lang.reflect.Method.invoke(Method.java:515)
07-16 00:12:56.305: E/AndroidRuntime(1104): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-16 00:12:56.305: E/AndroidRuntime(1104): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-16 00:12:56.305: E/AndroidRuntime(1104): at dalvik.system.NativeStart.main(Native Method)

我总是在第一个条件下出错。谁能帮我解决一下?

最佳答案

I always get null in Device and Connection when I call this method via Service.

当然。这是您在 IncomingCallService 上调用 startService() 的代码:

intent = new Intent(context, IncomingCallService.class);
context.startService(intent);

您会注意到您没有在此 Intent 上放置任何额外内容。因此,当您尝试将它们从 onStartCommand() 中交付给服务的 Intent 中拉出时,将不会有额外的内容。如果您希望在此 Intent 上添加额外内容,您需要输入 putExtra() 调用来执行此操作。

关于android - 将 Twilio 的传入调用代码从 Activity 实现到服务类中,但每次都会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31376053/

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