简体   繁体   English

Android AlarmManager不起作用

[英]Android AlarmManager doesn't work

I searched on the forum but it works for others. 我在论坛上搜索过,但对其他人有用。 What is wrong in my AlarmManager? 我的AlarmManager有什么问题? I want to call the CallDataSend class in every minute 我想每分钟调用一次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();
   }
}

the CallDataSend class: CallDataSend类:

public final class CallDataSend extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();

}
}

and xml file: 和xml文件:

<receiver
        android:name="CallDataSend"
        android:enabled="false">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"></action>
        </intent-filter>
     </receiver>

I believe you should make the receiver enabled before it can be instantiated by the system. 我相信您应该先使能接收器,然后才能由系统实例化它。

<receiver
        android:name="CallDataSend"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"></action>
        </intent-filter>
     </receiver>

You also have to name your receiver with ".YourClass" instead of "YourClass" if that class is in the same package declared in your manifest's attributes or with "YourCompletePackage.YourClass" if it is not. 您还可以用“.YourClass”而不是‘YourClass’来命名你的接收机如果类是在您的清单中的属性或‘YourCompletePackage.YourClass’宣称,如果它是不一样的包。

Ex: in your case, 例如:就您而言,

<receiver
    android:name=".CallDataSend"
    android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"></action>
    </intent-filter>
 </receiver>

in another case, 在另一种情况下,

<receiver
    android:name="com.example.CallDataSend"
    android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"></action>
    </intent-filter>
 </receiver>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM