简体   繁体   English

AlarmManager和接收方Android Studio Java发出多个通知

[英]multiple notification by AlarmManager and receiver Android Studio Java

i set two different alarms that should make 2 notifications 我设置了两个不同的警报,应该发出2条通知

8:34:0 pm create notification 1 with title and msg.. 晚上8:34:0创建带有标题和消息的通知1。

8:34:10 pm create notification 2 with title and msg... 晚上8:34:10创建带有标题和消息的通知2 ...

the problem is they both appear together at 8:34:00 pm (at the first alarm time (both))!!! 问题是它们都在晚上8:34:00一起出现(在第一个警报时间(均为))!!!

what i did:- 我做了什么:-

1- i made a broadcast class with notification manager to build notification 1-我与通知管理器进行了广播课程来构建通知

2- main activity created alarmManager and calendar instance to set time for alarm. 2-主要活动创建了alarmManager和日历实例以设置警报时间。

    public class MainActivity extends AppCompatActivity {


        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    // Creating alarm method(requestcode,hours,min,seconds)
            setAlarmo(1,20,34,0);
            setAlarmo(2,20,34,10);

        }

    // the Alarm Method
        public void setAlarmo(int reqcode, int hour,int minute,int second){


            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY,hour);
            calendar.set(Calendar.MINUTE,minute);
            calendar.set(Calendar.SECOND,second);

            long timeinMillis = calendar.getTimeInMillis();

            Intent intent = new Intent(getApplicationContext(),AlertRec.class);
            intent.putExtra("reqcode",reqcode);

PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),reqcode,intent,PendingIntent.FLAG_ONE_SHOT);

            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
            alarmManager.set(alarmManager.RTC_WAKEUP,timeinMillis,pendingIntent);

        }

    }

@ @

// AlertClass receiver extending broadcastReceiver
    public class AlertRec extends BroadcastReceiver {

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

// Creating receiveAlarm method(requestCode,Context,intent,title,text for notification)

//receiving first Alarm from MainActivity.Class 
         recAlarmo(1, context,intent,"title 1","im msg 1");


//receiving Second Alarm from MainActivity.Class     
         recAlarmo(2,context,intent,"title 2","im msg 2");
    }


    // receiving Alarms Method and Creating Notifications

    public void recAlarmo(int reqcode , Context context,Intent intent,String title,String msg){


     intent = new Intent(context,MainActivity.class);

     NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
     PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(),reqcode,intent,PendingIntent.FLAG_ONE_SHOT);
     builder.setContentTitle(title);
     builder.setSmallIcon(android.R.drawable.star_on);
     builder.setContentText(msg);
     builder.setAutoCancel(true);
     builder.setContentIntent(pendingIntent);
     builder.setDefaults(NotificationCompat.DEFAULT_SOUND);

     NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

     nm.notify(reqcode,builder.build());

    }
    }

@ @

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


        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            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=".AlertRec"></receiver>
        </application>
    i solved it !
    ------------------------------

    in MainActivity 

    1-create a method that takes int number
    and inside the method make a pending intent and within the intent of the pending intent
     pass the int number as intent.putExtra("id",number);

public void Method(int id ,int hour , int minute, int seconds){

 Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY,hour);
            calendar.set(Calendar.MINUTE,minute);
            calendar.set(Calendar.SECOND,second);

 Intent alertIntent = new Intent(getApplicationContext(), AlertRec.class);
            alertIntent.putExtra("id",id);
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),PendingIntent.getBroadcast(getApplicationContext(),id,alertIntent,PendingIntent.FLAG_UPDATE_CURRENT));

}

    ----------------------------------------------------
    2- at the Receiver class 


    //override the OnReceive method

    onReceive(Context context,Intent intent ){

     int Checker =intent.getExtras.getInt("id");

             if(Checker == 1 ) {

                 // do anything you want for the intent of the pending intent at MainActivity that have int id  = 1;
            //your method here

             }else if(Checker == 2) {

                 // do anything you want for the intent of the pending intent at MainActivity that have int id  = 2;
            // your method here

             }else{
    }

    }

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

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