- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在实现一个具有服务和 Activity 的 android 软件。我根据 youtube 视频编写了一些代码。但是当我单击开始按钮时,运行按钮它们工作正常。但是在我重新启动服务后(通过按停止然后再次按启动)程序崩溃了。所以任何人都可以帮我解决这个问题。
在 android 中还有什么方法可以在服务和 Activity 之间进行双向通信吗?(例如:- 服务获取 GPS 位置并按 0.001 更改位置,然后 ui 自动显示编辑后的位置)
请有人帮我解决这个问题。
我的代码:-
MyActivity.java
package com.example.autocomplete.servicemessenger;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MyActivity extends Activity {
boolean status=false;
Messenger msngr=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void startMethod(View v){
if (status){
Toast.makeText(getApplicationContext(), "already started", Toast.LENGTH_SHORT).show();
}
else {
Intent i = new Intent(this, MyService.class);
bindService(i, mConnection, Context.BIND_AUTO_CREATE);
status = true;
Toast.makeText(getApplicationContext(), "start", Toast.LENGTH_SHORT).show();
}
}
public void stoptMethod(View v){
if (status) {
Intent i = new Intent(this, MyService.class);
unbindService(mConnection);
stopService(i);
mConnection = null;
status = false;
Toast.makeText(getApplicationContext(), "stop", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "already stopped", Toast.LENGTH_SHORT).show();
}
}
public void runMethod(View v){
Message ms=Message.obtain(null,1,0,0,0);
String s="This is the message by Activity";
Bundle bn=new Bundle();
bn.putString("my_string",s);
ms.setData(bn);
try {
msngr.send(ms);
} catch (RemoteException e) {
e.printStackTrace();
}
}
ServiceConnection mConnection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
msngr=new Messenger(service);
status=true;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
msngr=null;
status=false;
Toast.makeText(getApplicationContext(),"Disc",Toast.LENGTH_SHORT).show();
}
};
}
我的服务.java
package com.example.autocomplete.servicemessenger;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.text.format.Time;
import android.widget.Toast;
import java.util.logging.Handler;
/**
* Created by NRV on 9/3/2014.
*/
public class MyService extends Service {
int val=1;
@Override
public IBinder onBind(Intent intent) {
return msgnr.getBinder();
}
@Override
public boolean onUnbind(Intent intent) {
stopSelf();
return super.onUnbind(intent);
}
class MessageHandler extends android.os.Handler{
public void handleMessage(Message msg){
if (msg.what==val){
Toast.makeText(getBaseContext(),"I am toast in service",Toast.LENGTH_SHORT).show();
Bundle bundle=msg.getData();
String msg_snt=bundle.getString("my_string");
Toast.makeText(getApplicationContext(),"I recv : "+msg_snt,Toast.LENGTH_SHORT).show();
}
else{
super.handleMessage(msg);
}
}
}
Messenger msgnr=new Messenger(new MessageHandler());
}
activity_my.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">
<TextView
android:text="@string/hello_world"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_marginTop="93dp"
android:layout_toLeftOf="@+id/button2"
android:onClick="startMethod"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:id="@+id/button2"
android:layout_below="@+id/button"
android:layout_alignParentRight="true"
android:layout_marginRight="106dp"
android:onClick="stoptMethod"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Run"
android:id="@+id/button3"
android:layout_below="@+id/button2"
android:layout_alignParentLeft="true"
android:onClick="runMethod"/>
</RelativeLayout>
最佳答案
当您在 stopMethod(View v) 中设置 mConnection=null 时,以及当您尝试从 startMethod(View v)< 绑定(bind)服务时/strong> 它正在尝试将服务与 mConnection(为空) 绑定(bind)。因此它正在崩溃。修复:注释掉 stoptMethod(View v) 中的 mConnection=null。也没有必要
Intent i = new Intent(this, MyService.class);停止服务(我);在 stopMethod(View v)
中当应用程序组件通过调用 bindService() 绑定(bind)到服务时,服务被“绑定(bind)”。绑定(bind)服务提供了一个客户端-服务器接口(interface),允许组件与服务交互、发送请求、获取结果,甚至通过进程间通信 (IPC) 跨进程进行。绑定(bind)服务仅在另一个应用程序组件绑定(bind)到它时运行。多个组件可以一次绑定(bind)到服务,但是当所有组件都解除绑定(bind)时,服务就会被销毁。
更多信息请访问bound-services和 Services .
关于Android Activity 服务通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25653149/
大多数语言都支持双向进程通信。例如,在 Python 中,我可以(草率地)执行以下操作: >>> from subprocess import * >>> p = Popen('nslookup',
致力于使用 C++ 在 arduino 和 PC (Win 7) 之间进行通信。使用 WriteFile 和 ReadFile 创建通信或简单地发送或接收数据没有问题。但是当我想以某种方式“协调”沟通
我们正在开发一个基于微服务的应用程序。它们将使用 Helm Package Manager 部署到 kubernetes,并且它们都存储了自己的存储库和 helm chart。以下是我们微服务的名称。
我正在开发一个大型 MVVM 应用程序。我为此使用了 MVVM 轻量级工具包。该应用程序就像一个带有后退和前进按钮的网络浏览器。主视图是一个用户控件。我在主视图用户控件中放置了后退和前进按钮。主视图又
我在 java 和 freepascal(lazarus) 应用程序之间的通信有问题。我使用套接字。它们正确连接。一切都很顺利,直到我想从一个应用程序向另一个应用程序发送一些东西。在java而不是“a
我已经使用客户端套接字和服务器套接字使用C#编写了群聊。 当我使用VS 2017在自己的PC中运行程序(服务器和客户端)时,客户端和服务器之间的通信工作正常。 当我在笔记本电脑中运行客户端程序,并在自
Kubernetes 中两个不同 Pod 之间的通信是如何发生的? 就我而言,我有两个 Pod:前端和后端,它们都有不同的容器。 我希望我的前端 pod 与后端 pod 通信,但我不想使用后端 pod
我正在尝试在浏览器中嵌入的 flash 实例与在 C# WinForms 应用程序中运行的 flash 实例之间进行通信...我收到一个编译错误,内容为: 1119 Access of possibl
鉴于网络上缺乏信息,请问一个问题:我要在 Android 中创建一个应用程序,使用一个数据库应用程序 rails 。为此,我需要一个手动 session 。所以如果有人准备好了示例/教程显示通信 an
我正在编写一个应用程序,它将通过 MySQL 数据库对用户进行身份验证。我已经用 Java (android) 编写了它,但现在正在移植到 Windows 手机。 PHP 文件使用 $get 然后回显
是否可以通过互联网在两个不同设备上的两个不同应用程序之间建立通信。我想从设备 A 上的应用程序点击一个设备 B 上的应用程序,然后从设备 B 上的应用程序获取数据到设备 A 上的应用程序。如果可能,如
这是脚本: 它被放置在其他网站上。 com 并显示一个 iframe。如果有人点击 iframe 中的某个内容,脚本应该将一个 div 写入 othersite 。 com. 所以我的问题是如何做到
你好我是 php 的新手,我用 c++ 编写了整个代码并想在 php 中使用这段代码。所以我为我的代码制作了 dll 以使用它。但是我不能在 php 中使用这个 dll,可以谁能给我完整的代码来使用
我确定之前已经有人问过(并回答过)此类问题,所以如果是这样,请将我链接到之前的讨论... 在 C++ 中,假设我有一个 ClassA 类型的对象,其中包含一个 ClassB 类型的私有(private
我正在尝试使用 ATmega32 进行串行通信。首先,我使用 RS232,使用 USB-to-RS232 建立使用串行终端的接收和传输(在我的例子中是 tera 术语)。无论我从串行终端 Atmega
我找不到适用于 Ruby 的 SSL 实现。 我的部分项目需要服务器和客户端之间的安全通信链接,我希望为此使用 SSL 以创建安全 session 。 谢谢 最佳答案 如果你使用 Ruby 1.9.x
我正在尝试在客户端/服务器之间进行 SSL 通信。 到目前为止,我已经从 keystore 创建了 java.security.cert.X509Certificate。接下来我应该怎么做才能使这次沟
我在与 Windows 上的 USB 设备 通信时遇到问题。我不能使用 libusb 或 WinUSB,因为我有一个特定的驱动程序(Silabs USB 到 UART,这是一个 USB 到串口的桥接器
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我发现 xcom 实际上是将数据写入数据库并从其他任务中提取数据。我的数据集很大,将其腌制并写入数据库会导致一些不必要的延迟。有没有办法在不使用 xcom 的情况下在同一 Airflow Dag 中的
我是一名优秀的程序员,十分优秀!