如何取消重复警报并安排在5秒后每隔一段时间点火?

时间:2016-09-12 19:55:51

标签: android broadcastreceiver alarmmanager

我使用广播接收器获取重复通知,以便在接收器切断呼叫或进入空闲状态后每隔5秒通知用户。 **

MainActivity.java

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn1 = (Button) findViewById(R.id.callbutton);

    PhoneCallListener phoneListener = new PhoneCallListener();
    TelephonyManager telephonyManager = (TelephonyManager) this
            .getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(phoneListener,  PhoneStateListener.LISTEN_CALL_STATE);
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:0000000000"));

            startActivity(callIntent);
        }
    });
}

private class PhoneCallListener extends PhoneStateListener {
    String LOG_TAG = "LOGGING 123 CHECK PHONE CALL";
    private boolean iscallConnect = false;
    private boolean makeCallOnce = false;

    public void onCallStateChanged(int state, String incoming_no) {
        if (TelephonyManager.CALL_STATE_RINGING == state) {
            // active
            Log.i(LOG_TAG, "RINGING");
            Toast.makeText(MainActivity.this, "RINGING",      Toast.LENGTH_SHORT).show();
            iscallConnect = true;
            makeCallOnce = true;
        }
        if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
            // active
            Log.i(LOG_TAG, "OFFHOOK");
            Toast.makeText(MainActivity.this, "OFFHOOK STATE", Toast.LENGTH_SHORT).show();
            iscallConnect = true;
            makeCallOnce = true;
        }
        if (TelephonyManager.CALL_STATE_IDLE == state) {
            Log.i(LOG_TAG, "CALL STATE IDLE");
            Toast.makeText(MainActivity.this, "IDLE STATE", Toast.LENGTH_SHORT).show();

            if (makeCallOnce) {
                alertNotification();
                Toast.makeText(MainActivity.this, "CALL DISCONNECTED", Toast.LENGTH_SHORT).show();

            }
        }
    }

    public void alertNotification() {
        Toast.makeText(this, "Alert Notification", LENGTH_LONG).show();
        alarmIntent = new Intent(MainActivity.this, AlertReceiver.class);
        pendingIntent1 = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);
        manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        int interval = 5000;
        manager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,AlarmManager.INTERVAL_FIFTEEN_MINUTES,
            AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent1);
        Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
        isButtonPressed = true;
    }

    public void stopAlertNotification(View v) {
        manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        manager.cancel(pendingIntent1);
        Toast.makeText(this, "Alarm Cancelled", Toast.LENGTH_SHORT).show();
    }
}

activity_main.xml中

  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Call"
    android:textSize="25sp"
    android:gravity="center"
    android:layout_marginTop="25dp"
    android:id="@+id/callbutton"
     />
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Cancel Reminder"
    android:textSize="25sp"
    android:onClick="stopAlertNotification"
    android:gravity="center"
    android:layout_below="@+id/callbutton"
    android:id="@+id/cancelbutton"
    />
    </RelativeLayout>

AlertReceiver.java

    public class AlertReceiver extends BroadcastReceiver {
    NotificationManager notificationManager;

    @Override
    public void onReceive(Context context, Intent intent) {
        createNotification(context,"Times up "," 5sec has passed","Alert");

    }

        public void createNotification(Context context,String msg,String    msgtext,String msgAlert)
    {
        Long alertTime=new GregorianCalendar().getTimeInMillis()+5*1000;
        Toast.makeText(context, "I am running ", LENGTH_LONG).show();
        Intent intent=new Intent(context, MainActivity.class);
        NotificationCompat.Builder mbuilder=new       NotificationCompat.Builder(context)
            .setContentTitle(msg)
            .setTicker(msgAlert)
            .setContentText(msgtext)
            .setSmallIcon(R.drawable.fail);
        PendingIntent pendingIntent=PendingIntent.getActivity(context, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        mbuilder.setContentIntent(pendingIntent);
        mbuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
        mbuilder.setAutoCancel(true);
        NotificationManager notificationManager=(NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,mbuilder.build());
    }

    public void stopNotification()
    {
        notificationManager.cancel(0);
    }
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".AlertReceiver"/>
</application>

0 个答案:

没有答案
相关问题