android报警管理器每15分钟运行一次后台服务

时间:2021-06-09 01:05:54

hey there i am trying to run android background service every 15 minutes using alaram manager but i am unable to do so i don't what mistake and i can't figure out whats wrong in my code its not working

嘿那里我试图使用alaram经理每隔15分钟运行一次android后台服务,但我无法这样做我不知道什么错误,我无法弄清楚我的代码中哪些错误不起作用

try {

        //Create a new PendingIntent and add it to the AlarmManager
        Intent intent = new Intent(this, RingAlarm.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
            12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am =
            (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
                3000,pendingIntent);

      } catch (Exception e) {
          Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();

      }




RingAlaram.class
public class RingAlarm extends Service { 
      public void onCreate() {  
                  Toast.makeText(getApplicationContext(), "hi there", Toast.LENGTH_LONG).show();
                }

        @Override
        public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return null;
        }



}

and last my manifest

最后我的清单

   <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.alarmmanagerexample.AlarmManagerExample"
            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=".RingAlarm"
            android:exported="false" />
    </application>

2 个解决方案

#1


You should use getService instead of getActivity and pass the application context (getApplicationContext) instead of activity context (this).

您应该使用getService而不是getActivity并传​​递应用程序上下文(getApplicationContext)而不是活动上下文(this)。

    //Create a new PendingIntent and add it to the AlarmManager
    Intent intent = new Intent(getApplicationContext(), RingAlarm.class);
    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 12345, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 3000, pendingIntent);

RingAlaram.class

public class RingAlarm extends Service { 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(getApplicationContext(), "hi there", Toast.LENGTH_LONG).show();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
}

Also set flag to PendingIntent.FLAG_UPDATE_CURRENT instead of CANCEL And set service declaration on manifest to <packagename>.RingAlarm might be com.example.alarmmanagerexample.RingAlarm in your case (I don't know your project structure).

同时将标志设置为PendingIntent.FLAG_UPDATE_CURRENT而不是CANCEL并将清单上的服务声明设置为 .RingAlarm可能是com.example.alarmmanagerexample.RingAlarm在你的情况下(我不知道你的项目结构)。

#2


Change getActivity to getService

将getActivity更改为getService

Intent intent = new Intent(this, RingAlarm.class);
        PendingIntent pendingIntent = PendingIntent.getService(this,
            12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am =
            (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
                3000,pendingIntent);

      } catch (Exception e) {
          Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();

      }

#1


You should use getService instead of getActivity and pass the application context (getApplicationContext) instead of activity context (this).

您应该使用getService而不是getActivity并传​​递应用程序上下文(getApplicationContext)而不是活动上下文(this)。

    //Create a new PendingIntent and add it to the AlarmManager
    Intent intent = new Intent(getApplicationContext(), RingAlarm.class);
    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 12345, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 3000, pendingIntent);

RingAlaram.class

public class RingAlarm extends Service { 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(getApplicationContext(), "hi there", Toast.LENGTH_LONG).show();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
}

Also set flag to PendingIntent.FLAG_UPDATE_CURRENT instead of CANCEL And set service declaration on manifest to <packagename>.RingAlarm might be com.example.alarmmanagerexample.RingAlarm in your case (I don't know your project structure).

同时将标志设置为PendingIntent.FLAG_UPDATE_CURRENT而不是CANCEL并将清单上的服务声明设置为 .RingAlarm可能是com.example.alarmmanagerexample.RingAlarm在你的情况下(我不知道你的项目结构)。

#2


Change getActivity to getService

将getActivity更改为getService

Intent intent = new Intent(this, RingAlarm.class);
        PendingIntent pendingIntent = PendingIntent.getService(this,
            12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am =
            (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
                3000,pendingIntent);

      } catch (Exception e) {
          Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();

      }