Tag: android service

Android:如何从工作线程到服务进行通信

我已经创建了一个服务类和一个在一个单独的线程中执行的worker类。 我想在它们之间建立通信,因此工作人员可以将某些状态发送回服务。 我试图将我的工作Thread转换为HandlerThread并在服务端设置一个Handler ,但后来我不知道如何实际从工作人员发送消息。 看起来我无法理解这个概念。 这是我的课程(没有沟通逻辑): 服务类 package com.example.app; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class ConnectionService extends Service { protected ConnectionWorker thread; @Override public void onCreate() { super.onCreate(); // Creating a connection worker thread instance. // Not starting it yet. this.thread = new ConnectionWorker(); Log.d(this.getClass().getName(), “Service created”); } @Override public int […]

Android O – 后台服务限制无法按预期工作

我只是根据Android O背景限制测试我现有的Android应用程序。 在测试中,我发现了一种奇怪的行为。 所以基本上在我的应用程序中,我正在使用后台服务,我在第一个活动中启动它。 所以现在问题是一旦活动和后台服务开始,我正在使用后退按钮关闭我的活动。 因此,根据服务概念,它会在后台运行。 大约1分钟后, onDestroy()方法被调用后台服务,但服务仍然在运行。 理想情况下,根据文档,它应该被杀死。 所以,我不知道这是一个问题还是什么问题。 作为参考,我创建了一个反映相同场景的示例代码,如下所示: 脚步 开始申请 单击“开始后台服务”按钮。 使用后退按钮关闭应用程序。 HomeActivity.java package com.icpl.otest; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import com.icpl.otest.service.MyService; public class HomeActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); } public void onStartServiceClick(View view) […]

当Service在后台工作时,AsyncTask中的doInBackground从不调用

private class Test extends AsyncTask { @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); Log.d(“test”, “called1”); } @Override protected Void doInBackground(Void… params) { Log.d(“test”, “called2”); return null; } @Override protected void onPostExecute(Void result) { // TODO Auto-generated method stub super.onPostExecute(result); Log.d(“test”, “called3”); } } 并输出: 测试:called1 为什么其他方法永远不会在Service在后台工作时调用? 如果服务停止,那么所有方法调用和输出: 测试:called1 测试:called2 测试:called3

如何从没有主线程或活动的服务调用Speechrecognizer方法

我知道Stackoverflow和其他网站上有很多关于SpeechRecognizer类的链接以及如何实现它。 我经历了其中几个并搜索了几个小时。 虽然在服务中有实现的例子,但它们似乎对我不起作用。 我的应用程序有一个活动,但它只用于用户设置,并且在我的服务运行时不运行,这是与系统分开调用的。 因此,为了运行/调用SpeechRecognizer类的方法,我的服务没有任何主要的应用程序线程。 正如在android文档中所述, 这里 : ‘必须仅从主应用程序线程调用此类的方法。 当我运行我的代码,包括我的服务类中的SpeechRecognizer.startListening(mSpeechRecognizerIntent)方法时,我收到以下错误: java.lang.RuntimeException:SpeechRecognizer只能从应用程序的主线程中使用 有没有办法解决这个问题? 我已经读过SpeechRecognizer可以“在后台或服务中运行时识别语音”,我发现这里: 在Android中使用语音识别的比较:通过Intent还是on-thread? 所以这基本上是我想要的,我似乎无法启动SpeechRecognizer,因为我得到了上述错误。 你能否告诉我是否有必要在应用程序的主线程中调用以下方法(我没有): mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this); mSpeechRecognizer.setRecognitionListener(new SpeechRecognitionListener()); 我的代码基本上与此链接的顶部答案中的代码相同:(没有IncomingHandler类,我相信它只用于控制监听器的时间) Android语音识别作为Android 4.1和4.2上的服务 非常感谢一些帮助,提前谢谢。 这是我的第一个Android应用程序,所以我对它很新,请耐心等待;)

Android Service(“startService&stopService”)和Play中的播放/停止按钮

我正在开发一个Android应用程序,我需要制作一个按钮,这将是Android服务播放和停止的播放/停止按钮。 播放按钮用于startActivity(); 停止按钮用于stopActivity(); 我该怎么做?

AlarmManager从不在AlarmReceiver / BroadcastReceiver中调用onReceive

我仍然无法触发我的AlarmReceiver类’onReceive方法。 这个实现有什么问题吗? 所有这一切应该是等待一段时间(最好是6天),然后弹出一个通知。 (你能相信没有内置系统吗?crontab任何人!?) MyActivity和BootReceiver都在必要条件下设置了警报。 AlarmService启动通知。 并且AlarmReceiver 应该捕获警报并启动AlarmService,但它从未捕获过该广播,并且无论我做什么都不会。 哦,我一直在测试我的Droid X,2.3.4。 项目是针对API 8构建的。 PS大部分内容改编自http://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/ ———— MyActivity.java ———— public class MyActivity extends Activity implements SensorEventListener { private void setupAlarm() { Log.i(TAG, “Setting up alarm…”); AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, new Intent(context, AlarmReceiver.class), 0); // Get alarm trigger time from prefs Log.i(TAG, “Getting […]

以明确的意图NullPointerException启动Android IntentService

我正在尝试从主活动中的clickhandler启动IntentService 。 我现在正在研究Intents但还没有完全掌握它。 我不确定我应该如何在这里实例化我的意图并将其传递给startService 。 我不知道为什么我要做这样的事情,在我的服务中不会使用Intent ,我知道。 我不知道如何调试这个问题。 从点击回调: this.commute = new Commute(); locationService = new LocationService(); locationService.setCommute(commute); // com.orm.SugarApp@8b2e18f is this.getApplicationContext() … Context context = this.getBaseContext(); System.out.println(“!!!!!!!!!!!!!!!!!!!!!”); System.out.println(context); Intent intent = new Intent(context, LocationService.class); System.out.println(locationService); System.out.println(intent); locationService.startService(intent); // <—- OFFENDING LINE System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@"); chronometer.setBase(SystemClock.elapsedRealtime()); chronometer.start(); commute.start(); 回溯之前的logcat,没有任何对象为null …: 01-27 09:53:44.465 2718-2718/org.skyl.commutetracker I/System.out﹕ !!!!!!!!!!!!!!!!!!!!! 01-27 09:53:44.466 […]

OnHandleIntent()没有在IntentService中调用

我知道之前已经问过这个问题,但是我已经找到了我能找到的所有答案,但仍然无法解决问题。 问题是当BroadcastReceiver启动时,不会调用IntentService onHandleIntent()。 奇怪的是构造函数运行(我可以通过Log输出看到)。 这是我的代码: NoLiSeA.class (此类包含启动我的服务的BroadcastReceiver) public void toProcess(StatusBarNotification sbn) { LocalBroadcastManager.getInstance(this).registerReceiver(notificationForwarder, new IntentFilter(“to_forward”)); Intent intent = new Intent(“to_forward”); intent.putExtra(“sbn”, sbn); LocalBroadcastManager.getInstance(this).sendBroadcast(intent); Log.i(“NoLiSe.TAG”, “toProcess”); } private BroadcastReceiver notificationForwarder = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.i(“NoLiSe.TAG”, “BroadCastReceiver.onReceive”); Intent i = new Intent(context, CoreTwoA.class); i.putExtras(intent); startService(i); } } }; CoreTwoA.class […]

在`Service`类中运行新的`runnableThread`时出现java.lang.ClassCastException

我有一个应用程序,包含一个broadcastReceiver ,它监听所有新收到的短信。 如果接收到任何新的SMS,则此BroadcastReceiver启动运行GPS并返回坐标的后台Service ,此坐标检索需要运行一个新线程,在Service类中没有getApplicationContext()所以我使用’getContentBase()’启动新线程这是代码 ((Activity)getBaseContext())。runOnUiThread(new Runnable(){ ((Activity)getBaseContext()).runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener = new MyLocationListener(); locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 5000, 10, locationListener); } } 我得到了这个例外 E/AndroidRuntime(24700): java.lang.ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity 这是接收器在清单中定义的方式 以下是服务在BroadcastReceiver启动方式 @Override public void onReceive(Context context, Intent intent) { […]

Android:如何在应用程序执行其他工作时持续保存传感器数据

我刚接触到android。 我正在开发一个应用程序,以持续保存传感器的数据,然后拍摄照片。 事实上,我想在拍照时开始将加速度计数据保存到文件中,或者屏幕显示为黑屏。 我已达到这一点,我的应用程序在触摸按钮时保存传感器数据,但它只执行一次。 如何让它连续保存数据而不影响我的应用程序的其他部分运行? 我希望它保存数据,而我正在我的应用程序中拍照,当屏幕被遮挡,而我已经离开我的应用程序(例如应用程序暂停) plz hlp thnx。 这是我的代码 private OnClickListener saveFun =new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub try { File root = Environment.getExternalStorageDirectory(); if (root.canWrite()){ File f = new File(root, “test.txt”); FileWriter fw; if ( saveBtnCnt == 0){ fw = new FileWriter(f); saveBtnCnt = 1; } […]