如何在开机后使用广播接收器自动启动服务?

4

我正在使用安卓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>

接收器类中的代码:

   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);
  }

   }

服务类代码:

 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;
   }

}

在主活动中,我有代码来显示通知。但问题是,在模拟器中运行应用程序后,接收器类本身没有被触发……您能否建议我哪里错了?

类似的问题:https://dev59.com/zHE85IYBdhLWcg3wbS1h?rq=1 - StarsSky
确实!我尝试过那个,但对我没用。 - user3057567
那么在你的问题中告诉我们。[你尝试过什么?](http://mattgemmell.com/what-have-you-tried/) - nhaarman
@Niek 谢谢回复。我更新了代码,你能帮忙看一下吗? - user3057567
1个回答

17

请按照步骤逐一进行。

创建此Receiver类:

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);
    }   

}

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>

1
谢谢您展示代码…但是这对您有用吗?我按照您说的做了,但是对我不起作用 :( - user3057567
1
但是通过这段代码,我已经完成了三个实时客户项目。你能展示一下你的代码吗? - Satyaki Mukherjee
1
在模拟器中运行应用后,接收器类本身没有被触发...你能否建议我哪里做错了?为了触发接收器,你需要启动设备。请先从真实设备检查你的代码。 - Satyaki Mukherjee
1
非常感谢,它对我有用 :-). 再次感谢!! - user3057567
3
在清单文件中加入<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />之前,这个功能是无法正常工作的。 - Michael Kern
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接