设备启动完成后启动服务
我正在做一个应用程序,我想在设备启动完成时拨打一个号码。我的代码是这样的:
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("**inside onRecevier");
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.test.app.TestService");
context.startService(serviceIntent);
}
首先我创建了BroadcastReceiver。我在清单文件中注册了这个接收器,如下所示:
<receiver android:name="TestReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
在接收器中,我调用了以下服务:
public class TestService extends Service{
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
System.out.println("**inside onCreate");
super.onCreate();
Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
Intent call = new Intent(Intent.ACTION_CALL,Uri.parse("tel:+5555"));
startActivity(call);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("**inside onDestroy");
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
System.out.println("**inside onStart");
super.onStart(intent, startId);
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}
}
当我在启动应用程序后尝试启动设备时,强制关闭。如何在安卓中做到这一点? 提前感谢
I am doing an application in which I want to call a number when that device boot completed. My code is like this:
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("**inside onRecevier");
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.test.app.TestService");
context.startService(serviceIntent);
}
First I created BroadcastReceiver. I registered this receiver in manifest file like this:
<receiver android:name="TestReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
In a receiver I called the below service:
public class TestService extends Service{
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
System.out.println("**inside onCreate");
super.onCreate();
Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
Intent call = new Intent(Intent.ACTION_CALL,Uri.parse("tel:+5555"));
startActivity(call);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("**inside onDestroy");
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
System.out.println("**inside onStart");
super.onStart(intent, startId);
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}
}
when I tried to boot a device after booting the application getting force close. How to do this in android??
Thanx in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在从服务启动活动之前添加 NEW_TASK 标志:
这解释了:
此外,您必须持有权限:
RECEIVE_BOOT_COMPLETED
< /a>CALL_PHONE
正如 Waqas 提到的,您最好从
onReceive
启动服务,例如:确保您已完成了我所说的所有操作,如果您仍然遇到问题,那么编辑您的问题并从强制关闭中粘贴 logcat 将会很有帮助。
You need to add the NEW_TASK flag before starting an activity from a service:
This explains:
Also, you must hold the permissions:
RECEIVE_BOOT_COMPLETED
CALL_PHONE
And as Waqas mentioned, it would be better for you to start your service from your
onReceive
like:Make sure you've done all that I've stated, and if you continue to have an issue, then it would be helpful if you edit your question and paste the logcat from the force close.
将你的 onReceive 更改为此
change your onReceive to this
强制关闭的原因有多种。辨别哪个的最好方法是查看日志。它应该准确地告诉您抛出了什么异常,并提示您如何解决问题。
There's a variety of reasons why you'd get a Force Close. The best way to tell which is to look at the log. It should tell you exactly what exception is being thrown, and give you a hint as to how to solve the problem.