Android 启动后如何使用广播接收器自动启动服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20919704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start a service automatically using broadcastreceivers after booting?
提问by user3057567
I am using android 4.4 version. How to start a service automatically using broadcastreceivers after booting? Thanks in advance. UPDATE: code in manifest file:
我正在使用 android 4.4 版本。启动后如何使用广播接收器自动启动服务?提前致谢。更新:清单文件中的代码:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.prashanthi.trial.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".WordService"></service>
<receiver android:name=".MyScheduleReceiver" android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
Code in receiver class:
接收器类中的代码:
public class MyScheduleReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
System.out.println("This is MyShedularReceiver");
Intent service = new Intent(context, WordService.class);
//service.setAction(RECEIVE_BOOT_COMPLETED);
context.startService(service);
}
}
Code for service class:
服务类代码:
public class WordService extends Service{
public void onCreate() {
super.onCreate();
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent
.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (5 * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in 5 seconds",
Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
In main activity I have code to display notifications. But the problem is, receiver class itself not triggered after running the app in emulator....can u please suggest me where I went wrong?
在主要活动中,我有代码来显示通知。但问题是,在模拟器中运行应用程序后,接收器类本身没有触发......你能告诉我我哪里出错了吗?
回答by Satyaki Mukherjee
Follow this step one by one.
一步一步地执行此步骤。
Create this Receiver class:
创建此接收器类:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootUpReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
/****** For Start Activity *****/
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
/***** For start Service ****/
Intent myIntent = new Intent(context, ServiceClassName.class);
context.startService(myIntent);
}
}
In AndroidManifest.xml
:
在AndroidManifest.xml
:
<receiver
android:enabled="true"
android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service
android:name=".ServiceClassName"
android:enabled="true"
android:exported="true" >
</service>