我正在开发我的第一款Android应用.我的应用程序中有三个活动,用户来回切换频繁.我还有一个远程服务,它处理一个telnet连接.应用程序需要绑定到此服务才能发送/接收telnet消息.
编辑 感谢BDLS提供的信息.我已根据您对使用bindService()作为独立函数或在startService()之间的区别的说明重新编写了我的代码,现在我只是在使用后退按钮之间循环时间歇性地获取泄漏错误消息活动.
我的连接活动有以下onCreate()和onDestroy():
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* Initialize the ServiceConnection. Note that this is the only place startService() is run.
* It is also the only time bindService is run without dependency on connectStatus.
*/
conn = new TelnetServiceConnection();
//start the service which handles telnet
Intent i = new Intent();
i.setClassName( "com.wingedvictorydesign.LightfactoryRemote", "com.wingedvictorydesign.LightfactoryRemote.TelnetService" );
startService(i);
//bind to the service
bindService(i, conn, 0);
setContentView(R.layout.connect);
setupConnectUI();
}//end …Run Code Online (Sandbox Code Playgroud) 我有一个Activity调用在IDownloaderService.aidl中定义的服务:
public class Downloader extends Activity {
IDownloaderService downloader = null;
// ...
Run Code Online (Sandbox Code Playgroud)
在Downloader.onCreate(Bundle)中,我尝试使用bindService
Intent serviceIntent = new Intent(this, DownloaderService.class);
if (bindService(serviceIntent, sc, BIND_AUTO_CREATE)) {
// ...
Run Code Online (Sandbox Code Playgroud)
在ServiceConnection对象sc中,我做到了这一点
public void onServiceConnected(ComponentName name, IBinder service) {
Log.w("XXX", "onServiceConnected");
downloader = IDownloaderService.Stub.asInterface(service);
// ...
Run Code Online (Sandbox Code Playgroud)
通过添加各种Log.xx,我发现if(bindService(...))之后的代码实际上是在调用ServiceConnection.onServiceConnected之前 - 也就是说,当下载器仍然为null时 - 这让我遇到了麻烦.ApiDemos中的所有样本都通过仅在用户操作触发时调用服务来避免此时间问题.但是,在bindService成功之后,我该怎么做才能正确使用这个服务?如何可靠地等待ServiceConnection.onServiceConnected被调用?
另一个问题有关.是所有的事件处理程序:Activity.onCreate,任何View.onClickListener.onClick,ServiceConnection.onServiceConnected等实际在同一个线程中调用(在文档中提到的"主线程")?它们之间是否存在交错,或Android会安排所有事件逐个处理?或者,究竟什么时候实际上要调用ServiceConnection.onServiceConnected?完成Activity.onCreate或A.oC仍在运行时?
每次按下Back我的应用程序中的按钮,我都会在LogCat中收到此异常:
Activity已泄露最初绑定的ServiceConnection com.android.vending.licensing.LicenseChecker@471cc039
造成这种泄漏的代码onCreate()是:
mLicenseCheckerCallback = new MyLicenseCheckerCallback();
mChecker.checkAccess(mLicenseCheckerCallback);
Run Code Online (Sandbox Code Playgroud)
我如何摆脱这种泄漏?
我尝试不将MyLicenseCheckerCallback分配给成员,也许在活动开始时onPause()对回调的引用负责泄漏:
mChecker.checkAccess(new MyLicenseCheckerCallback());
Run Code Online (Sandbox Code Playgroud)
但这并没有摆脱泄漏.
更新:感谢@ zapl在下面的评论,我查看了Google的LicenseChecker.java:
/** Unbinds service if necessary and removes reference to it. */
private void cleanupService() {
if (mService != null) {
try {
mContext.unbindService(this);
} catch (IllegalArgumentException e) {
// Somehow we've already been unbound. This is a non-fatal error.
Log.e(TAG, "Unable to unbind from licensing service (already unbound)");
}
mService = null;
}
}
Run Code Online (Sandbox Code Playgroud)
起初我以为我可能会忽略称它,但我仔细检查了,我 …
在我的Android应用程序,我打电话都startService和bindService:
Intent intent = new Intent(this, MyService.class);
ServiceConnection conn = new ServiceConnection() { ... }
startService(intent)
bindService(intent, conn, BIND_AUTO_CREATE);
Run Code Online (Sandbox Code Playgroud)
后来,我尝试unbindService and停止服务`:
unbindService(conn);
stopService(intent);
Run Code Online (Sandbox Code Playgroud)
但是,我在调用时遇到异常unbindService.如果我删除此呼叫,该应用似乎通过stopService呼叫正常运行.
难道我做错了什么?我认为bindService呼叫必须与unbindService呼叫相关联,并且startService呼叫必须与stopService呼叫相关联.但是,这似乎并非如此.
在我的应用程序中,我认识到用户说"退出"或"关闭",应用程序应关闭.有了这段代码
SpeechRecognizer sr;
Map<String, Integer> dictionary;
private static final int EXIT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateDictionary();
SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(this);
sr.setRecognitionListener(this);
Intent voiceIntent = RecognizerIntent.getVoiceDetailsIntent(getApplicationContext());
sr.startListening(voiceIntent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void populateDictionary() {
dictionary = new HashMap<String, Integer>();
dictionary.put("exit", EXIT);
dictionary.put("close", EXIT);
}
@Override
public void onResults(Bundle results) {
ArrayList<String> …Run Code Online (Sandbox Code Playgroud) 我有几个 AndroidService想要绑定到我的 中Activity,所以我可以监控用户的几个操作。
为了能够绑定每个服务,我将有几个,ServiceConnection我的活动中是否需要几个 private ,如下所示?
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
GPSLocalBinder gpsBinder = (GPSLocalBinder) service;
PhotoLocalBinder photoBinder = (PhotoLocalBinder) service;
gpsService = gpsBinder.getService();
photoService = photoBinder.getService();
mGpsBound = true;
mPhotoBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mGpsBound = …Run Code Online (Sandbox Code Playgroud) android serviceconnection android-service android-service-binding
这是我的拳头stackoverflow帖子所以请对我温柔!我确信我正在尝试做的事情是可能的,这是我做过的事情(或者没做过的事情?)导致问题......我只是不确定那是什么东西.
我正在尝试做的事:
在我的应用程序同步并处理下载的数据时显示ProgressDialog.
问题:
ProgressDialog显示但不旋转(这使它看起来像已冻结),同步和处理发生,ProgressDialog关闭,应用程序继续正常.
我目前如何做到这一点:
创建ProgressDialog - 在我的活动中
执行同步 - 在我的服务
处理数据 - 在我的服务
Dismis中ProgressDialog - 在我的活动中
我尝试过的事情:
使用线程(下面的代码)使用AsynTask(如果需要可以提供代码)使用Handler(如果需要可以提供代码)
在花了很多时间寻找我的问题的答案后,似乎其他人有相同或类似的问题,并设法使用上述一个想法解决它.但是,我无法实现任何这些想法来解决我的特定问题.这让我确信这是我做错的事情......我只是不确定是什么.
我编写的原始代码并用作我所有尝试修复的基础:
首先
public class myActivity extends ListActivity {
private ProgressDialog dialog;
private boolean mIsBound;
private myService mBoundService;
private ServiceConnection mConnection;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*if we need to login start login activity for result*/
...
}
...
/*other methods*/
...
}
Run Code Online (Sandbox Code Playgroud)
然后
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch …Run Code Online (Sandbox Code Playgroud) 更新:
我刚刚在另一台设备上测试了我的应用程序,发现我确实在运行Android 4.4.2的Nexus 4上出现了错误,但没有在运行Android 4.0.4的Desire S上出现错误.他们都安装了当前的YouTube应用程序(5.3.32),这是使用API所必需的.
问题:为什么我会收到这些ServiceConnectionLeaked消息?(见下面的Logcat)
描述:
我正在使用YouTube Android Player API 1.0.0(https://developers.google.com/youtube/android/player/)使用以下适配器代码在ListView中加载视频缩略图:
private final Map<View, YouTubeThumbnailLoader> mThumbnailViewToLoaderMap;
public ListViewAdapter(Activity activity, int layoutId, List<Suggestion> suggestions) {
super(activity, layoutId, suggestions);
mThumbnailViewToLoaderMap = new HashMap<View, YouTubeThumbnailLoader>();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
String videoId = getYoutubeId();
// There are three cases here...
if (convertView == null) {
convertView = LayoutInflater.from(mActivity).inflate(R.layout.row_suggestion, parent, false);
holder = new ViewHolder();
holder.thumbnailView = (YouTubeThumbnailView) convertView.findViewById(R.id.youtubeThumbnail); …Run Code Online (Sandbox Code Playgroud) android listview thumbnails serviceconnection android-youtube-api
我正在尝试在 Azure DevOps 中创建发布管道。我在 Azure 中创建了一个应用程序服务资源,我想通过我的管道将我的 Web 应用程序部署到此应用程序服务。为此,我需要创建一个 ARM 服务连接。您能帮助我了解服务连接类型之间的区别吗?谢谢!
serviceconnection azure-active-directory service-principal azure-devops azure-managed-identity
我有一个应用程序在启动时使用文本转语音.一切似乎都很完美,我在运行应用程序时没有任何问题.但是,每次应用程序启动时,我都会收到一条LogCat错误,指出我的文本转语音出现了泄漏的ServiceConnection.虽然这不会导致功能问题,但我担心如果我继续忽略错误,将来会发生什么.我怀疑这只是错误的编码,但我没有经验来确定错位.我已经包含了LogCat和令人讨厌的java.
07-31 10:16:50.812: E/ActivityThread(27785): Activity com.example.com.proto1.menu has leaked ServiceConnection android.speech.tts.TextToSpeech$1@40d68398 that was originally bound here
07-31 10:16:50.812: E/ActivityThread(27785): android.app.ServiceConnectionLeaked: Activity com.example.com.proto1.menu has leaked ServiceConnection android.speech.tts.TextToSpeech$1@40d68398 that was originally bound here
07-31 10:16:50.812: E/ActivityThread(27785): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:932)
07-31 10:16:50.812: E/ActivityThread(27785): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:827)
07-31 10:16:50.812: E/ActivityThread(27785): at android.app.ContextImpl.bindService(ContextImpl.java:1109)
07-31 10:16:50.812: E/ActivityThread(27785): at android.content.ContextWrapper.bindService(ContextWrapper.java:370)
07-31 10:16:50.812: E/ActivityThread(27785): at android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:517)
07-31 10:16:50.812: E/ActivityThread(27785): at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:483)
07-31 10:16:50.812: E/ActivityThread(27785): at com.example.com.proto1.menu.onActivityResult(menu.java:255)
07-31 10:16:50.812: E/ActivityThread(27785): at android.app.Activity.dispatchActivityResult(Activity.java:4581)
07-31 10:16:50.812: E/ActivityThread(27785): at android.app.ActivityThread.deliverResults(ActivityThread.java:2817)
07-31 10:16:50.812: E/ActivityThread(27785): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2864) …Run Code Online (Sandbox Code Playgroud) android ×9
java ×2
android-lvl ×1
azure-devops ×1
binding ×1
google-play ×1
listview ×1
service ×1
thumbnails ×1
ui-thread ×1