【问题标题】:How to start a service automatically using broadcastreceivers after booting? [duplicate]启动后如何使用广播接收器自动启动服务? [复制]
【发布时间】:2014-01-04 10:25:22
【问题描述】:

我使用的是安卓 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;
   }

}

在主要活动中,我有显示通知的代码。但问题是,在模拟器中运行应用程序后接收器类本身没有被触发....你能告诉我我哪里出错了吗?

【问题讨论】:

标签: android service broadcastreceiver


【解决方案1】:

按照这一步一步一步来。

创建这个接收器类:

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>

【讨论】:

  • 感谢您显示代码..但这对您有用吗...我完全按照您说的尝试了,但对我不起作用:(
  • 但是通过这段代码我已经完成了三个实时客户端项目。请出示您的代码。
  • "在模拟器中运行应用程序后接收器类本身未触发....你能告诉我哪里出错了吗?"在模拟器中接收器将如何触发?要触发,您需要启动设备。请先在真机上检查您的代码。
  • 非常感谢它对我有用:-)。再次感谢!
  • 在我将 添加到清单之前没有工作
猜你喜欢
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多