我在论坛上搜索,但它适用于其他人。我的AlarmManager出了什么问题? 我想在每分钟调用CallDataSend类
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent alarmIntent = new Intent(this, CallDataSend.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
alarmIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (2 * 1000), pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
}
}
CallDataSend类:
public final class CallDataSend extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
}
}
和xml文件:
<receiver
android:name="CallDataSend"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
</intent-filter>
</receiver>
答案 0 :(得分:2)
我相信你应该在系统实例化之前启用接收器。
<receiver
android:name="CallDataSend"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
</intent-filter>
</receiver>
答案 1 :(得分:2)
您还必须使用&#34; .YourClass&#34; 而不是&#34; YourClass&#34; 为您的接收者命名,如果该类在在您的清单属性中声明的相同包,或者使用&#34; YourCompletePackage.YourClass&#34; ,如果不是。
例: 在你的情况下,
<receiver
android:name=".CallDataSend"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
</intent-filter>
</receiver>
在另一个案例中,
<receiver
android:name="com.example.CallDataSend"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
</intent-filter>
</receiver>