Android - 报警管理器不工作?

时间:2013-09-16 09:28:56

标签: android broadcastreceiver alarmmanager android-pendingintent

我正在尝试在单击按钮时触发警报。但它不起作用是什么问题。

AlarmSet.java

private void setAlarm() {   
    //Alarm 
    Toast.makeText(getApplicationContext(), "alerm set", Toast.LENGTH_LONG).show();


    Intent myIntent = new Intent(this, AlarmManager.class); PendingIntent        
    pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,0);

   AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
   alarmManager.set(AlarmManager.RTC, (System.currentTimeMillis(), pendingIntent);

    //Alarm END
}

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {

    System.out.println("ALARM SUCCESS");
    // TODO Auto-generated method stub
    Toast.makeText(context, "Don't panik but your time is up!!!!.",
            Toast.LENGTH_LONG).show();
    // Vibrate the mobile phone
    Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    vibrator.vibrate(2000);

    Intent service1 = new Intent(context, MyAlarmService.class);
    context.startService(service1);
} 
}

MyAlarmService.java

public class MyAlarmService extends Service 

{
private NotificationManager mManager;

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

@Override
public void onCreate() 
{
    // TODO Auto-generated method stub  
    super.onCreate();
}

@SuppressWarnings({ "static-access", "deprecation" })
@Override
public void onStart(Intent intent, int startId)
{
    super.onStart(intent, startId);

    mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(),CurrentParking.class);

    Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());

    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(this.getApplicationContext(), "Sweeper Stopper", "This is a test message!", pendingNotificationIntent);

    mManager.notify(0, notification);
}

@Override
public void onDestroy() 
{
    // TODO Auto-generated method stub
    super.onDestroy();
}

}

清单

  <application
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Black.NoTitleBar" >

    <service
        android:name="com.menthatech.sweeperstopper.MyAlarmService"
        android:enabled="true" />

    <receiver
        android:name="com.menthatech.sweeperstopper.AlarmReceiver"
        android:enabled="true"
        android:process=":remote" />

</application>

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg"
tools:context=".LandingPage" >

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/footer" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <EditText
            android:id="@+id/et_address"
            android:layout_width="fill_parent"
            android:layout_height="80dp"
            android:layout_marginTop="15dp"
            android:background="@drawable/edittext_background"
            android:gravity="center"
            android:inputType="textMultiLine"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/et_datefield"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_below="@+id/et_address"
            android:layout_marginTop="15dp"
            android:background="@drawable/edittext_background"
            android:focusable="false"
            android:gravity="center"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/et_sweepingTime"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_below="@+id/et_datefield"
            android:layout_marginTop="15dp"
            android:background="@drawable/edittext_background"
            android:focusable="false"
            android:gravity="center"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/et_alertTime"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_below="@+id/et_sweepingTime"
            android:layout_marginTop="15dp"
            android:background="@drawable/edittext_background"
            android:focusable="false"
            android:gravity="center"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/et_alertTime"
            android:layout_gravity="center"
            android:layout_marginTop="15dp"
            android:gravity="center"
            android:orientation="vertical" >

            <Button
                android:id="@+id/checkForSchedule"
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:layout_gravity="center"
                android:background="@drawable/edittext_background"
                android:gravity="center"
                android:text="Check For The Schedule"
                android:textColor="@android:color/white"
                android:textSize="18sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/setAlarm"
                android:layout_width="100dp"
                android:layout_height="40dp"
                android:layout_gravity="center"
                android:layout_marginTop="15dp"
                android:background="@drawable/edittext_background"
                android:gravity="center"
                android:text="Alarm"
                android:textColor="@android:color/white"
                android:textSize="18sp"
                android:textStyle="bold" />
        </LinearLayout>
    </RelativeLayout>
</ScrollView>

<LinearLayout
    android:id="@+id/footer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/home" />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/hometext" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/park" />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/parktext" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/mycar" />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/mycartext" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_settings"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/settings" />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/settingstext" />
    </LinearLayout>
</LinearLayout>

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

Intent myIntent = new Intent(this, AlarmManager.class);

在此行中将AlarmManager.class更改为AlarmReceiver.class

答案 1 :(得分:0)

可能是你的警报。 set方法有错误

变化

alarmManager.set(AlarmManager.RTC, (System.currentTimeMillis(), pendingIntent);

Calendar cal = Calendar.getInstance();
alarmManager.set(AlarmManager.RTC,cal.getTimeInMillis(), pendingIntent);

还要检查pendingIntent声明。

Custom BroadCast:

private void setAlarm() {   
    //Alarm 
    Toast.makeText(getApplicationContext(), "alerm set", Toast.LENGTH_LONG).show();


   Intent intent = new Intent();
   intent.setAction("com.example.Broadcast.start");
   sendBroadcast(intent);

    //Alarm END
}

现在,在清单

<receiver
            android:name="AlarmReceiver">

            <intent-filter>

                <action android:name="com.device.Broadcast.start" />

            </intent-filter>
</receiver>

现在,AlarmReceiver     公共类AlarmReceiver扩展了BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

 if(intent.getAction().equals("com.device.Broadcast.start")){
// Do the what you want
}

 }
 }
相关问题