使用数据库和服务进行警报以触发通知

时间:2014-04-01 07:11:08

标签: android sqlite

我正在开发一个应用程序,它将在特定时间触发通知。我正在尝试将时间存储在数据库中。还有一个服务来设置数据库中的警报。通知下一个警报必须设置。我尝试以下代码......但不工作..请帮助... 谢谢 .. 抱歉我的英语不好..

public class Alarm extends BroadcastReceiver 
{    
     @Override
     public void onReceive(Context context, Intent intent){   

         PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
         PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
         wl.acquire();

         // Put here YOUR code.
         Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example

         wl.release();
     }

     public void SetAlarm(Context context){

         AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
         Intent i = new Intent(context, Alarm.class);

         // Intent i = new Intent("mypackage.START_ALARM");
         PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
         System.out.println("Started");

         am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 2, pi); // Millisec * Second * Minute
     }

     public void CancelAlarm(Context context){
         Intent intent = new Intent(context, Alarm.class);
         PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
         AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
         alarmManager.cancel(sender);
     }
}

public class AutoStart extends BroadcastReceiver
{   

    Alarm alarm = new Alarm();

    @Override
    public void onReceive(Context context, Intent intent){   
        Toast.makeText(context, "Started", 3);
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
            alarm.SetAlarm(context);
        }
    }
}

服务

public class YourService extends Service
{
    Alarm alarm = new Alarm();

    public void onCreate(){
        super.onCreate();       
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
         alarm.SetAlarm(YourService.this);
         return START_STICKY;
    }


    public void onStart(Context context,Intent intent, int startId){
        alarm.SetAlarm(context);
    }

    @Override
    public IBinder onBind(Intent intent){
        return null;
    }
}

这是我的清单文件..

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testfont"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> 


    <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver  android:process=":remote" android:name="Alarm"></receiver>


        <receiver android:name=".AutoStart">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>            
    </application>

</manifest>

此程序是在2分钟后设置警报但在模拟器中测试时没有得到任何响应。如果这个工作,我打算编写数据库代码来获取SetAlarm方法内部的时间..这个想法是好还是不请帮助..

0 个答案:

没有答案