在android平台上启动服务
我正在使用 startService(Intent Intent) 方法启动服务。当我调用此函数时,它到达服务的onCreate,但无法调用onStartCommand。这是我的代码--
@Override
public void onReceive(Context context, Intent intent) {
// Send a text notification to the screen.
Log.e("mudit", "Action: " + intent.getAction());
try {
ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connManager.getActiveNetworkInfo();
Log.e("mudit", "getType: " + info.getType());
Log.e("mudit", "isConnected: " + info.isConnected());
if (info.isConnected()) {
Intent newinIntent = new Intent(context, service.class);
context.startService(newinIntent);
}
} catch (Exception e) {
e.printStackTrace();
Intent newinIntent = new Intent(context, service.class);
context.stopService(newinIntent);
}
}
服务代码--
package com.android.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class service extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
}
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "onStartCommand...", Toast.LENGTH_LONG).show();
return 1;
}
}
Manifest.xml --
<receiver class=".AReceiver" android:name=".AReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<service class=".service" android:name=".service"
android:enabled="true" android:icon="@drawable/icon">
</service>
I am starting a service using startService(Intent intent) method. When i call this function it reaches the onCreate of service but it is unable to call onStartCommand. Here is my code--
@Override
public void onReceive(Context context, Intent intent) {
// Send a text notification to the screen.
Log.e("mudit", "Action: " + intent.getAction());
try {
ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connManager.getActiveNetworkInfo();
Log.e("mudit", "getType: " + info.getType());
Log.e("mudit", "isConnected: " + info.isConnected());
if (info.isConnected()) {
Intent newinIntent = new Intent(context, service.class);
context.startService(newinIntent);
}
} catch (Exception e) {
e.printStackTrace();
Intent newinIntent = new Intent(context, service.class);
context.stopService(newinIntent);
}
}
Service Code --
package com.android.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class service extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
}
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "onStartCommand...", Toast.LENGTH_LONG).show();
return 1;
}
}
Manifest.xml --
<receiver class=".AReceiver" android:name=".AReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<service class=".service" android:name=".service"
android:enabled="true" android:icon="@drawable/icon">
</service>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
来源
Source
larsVogel 解决了这个问题(以及许多其他类似问题)这篇优秀的文章 。
这就是我如何修改他的代码来创建一个连接接收器,用于监视用户何时连接到 WIFI 网络,以便批量上传使用数据:
在清单文件中,放置一个接收器并在 < 的结束标记之前声明一个服务; /application>:
在名为 ConnMonitor.java 的单独文件中创建一个广播接收器类(请取消注释 Log 调用以便能够正确监视流),
最后创建一个服务 BatchUploadGpsData.java,如下所示:
这不是伪代码,这是实际代码,经过测试并在 android 2.2 及更高版本上运行。
测试此服务的方法是关闭并重新启动 Android 上的 WIFI 服务(关闭 wifi 路由器的电源也可以解决问题)。但此代码不会验证您是否有效连接到网络。为此,我建议您发出 httpclient 请求并检查调用结果。超出了本次讨论的范围。
注意:由于服务与 UI 在同一线程上运行,我强烈建议您根据您的具体需求在单独的线程或异步任务上实现正确的上传。您还可以在单独的线程上运行整个服务,但这再次不是本次讨论的范围,尽管这是这些情况下的标准做法。
larsVogel solves this problem (and many others like it) in this excellent post.
this is how i adapted his code to create a connectivity receiver that monitors when the user connects to a WIFI network so as to batch upload usage data:
in the Manifest file, place a receiver and declare a service right before the end tag for your < / application >:
create a broadcast receiver class in a separate file called ConnMonitor.java (please uncomment the Log calls to be able to properly monitor the flow)
and, finally, create a service BatchUploadGpsData.java like this:
this is not pseudocode, this is actual code, tested and running on android 2.2 and up.
the way to test this service is to shut down and restart your WIFI services on your android (powering off the wifi router will also do the trick). BUT this code does not verify if you are effectively connected to the net. for that, i recomend that you make an httpclient request and check out the result of the call. beyond the scope of this discussion.
NOTE: since services run on the same thread as the UI, i highly recommend that you implement the uploading proper on a separate thread or asynctask, depending your specific needs. you can also run the whole service on a separate thread, but that is once again not the scope of this discussion, despite being standard practice in these cases.
首先,您应该在
onStartCommand(..)
之前添加@Override
,然后确保 Android 项目的目标高于 2.0 。First you should add
@Override
beforeonStartCommand(..)
then make sure that the target for the Android project is higher than 2.0 .我相信,您无法访问任何 UI 组件,例如 Dialog,甚至服务中的 Toast。
试试这个。
I believe, that you cannot access any UI components like Dialog or even a Toast in a service.
try this.
首先,我建议将您的班级命名为其他名称,以避免以后出现混乱。其次,这是我对可用服务的清单调用的示例。我在调用服务等时使用完整路径名,因为它们与我的应用程序不在同一包中。
这是我的服务类的要点,
第三,确保对 onStartCommand() 使用 @Override。
First of all name your class to something else is my recommendation to avoid confusion down the line. Second here is an example of my manifest call of a service I have that works. I use full path names when calling services and such since they are not in the same package as my application.
Here is the gist of my service class,
Third make sure you use @Override to the onStartCommand().