在我的活动课上
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,System.currentTimeMillis(),2000, pendingIntent);
}
Run Code Online (Sandbox Code Playgroud)
我的onrecieve功能在alarmreciever类
@Override
public void onReceive(Context context, Intent intent)
{
//get and send location information
System.out.println("fired");
}
Run Code Online (Sandbox Code Playgroud)
我正在使用nexus 4,kitkat版本.我没有看到每2分钟发射任何一个接收函数.正在发生...任何帮助?谢谢
Run Code Online (Sandbox Code Playgroud)<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.alarmexample" android:versionCode="1" android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="20" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".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>
<receiver
android:name="com.example.AlarmExample"
android:exported="false" >
</receiver>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
我也把我的清单也放了.................................................
sam*_*sam 12
在setRepeating函数中,您应该对ELAPSED_REALTIME_WAKEUP使用SystemClock.elapsedRealTime().此外,您需要将2000更改为2*60*1000以指定间隔时间.
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(),
2*60*1000,
pendingIntent);
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
编辑:在您的清单文件中,您的收件人姓名中有一个拼写错误.将".AlarmReciever"更改为".AlarmReceiver".
<receiver
android:name=".AlarmReceiver"
android:exported="true" >
</receiver>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15575 次 |
| 最近记录: |