2017-08-09 9 views
-1

私は2つのアクティビティクラス、メインアクティビティ&評価バーのアクティビティを持っています。私はMyReceiverとして放送受信機クラスを持っています。私は時刻を20:00に設定しています。または通知を受け取るためにシステム時刻に応じて変更しますが、通知やログキャストのエラーは表示されません。何をする必要があるかを提案してください。以下は、私のクラスは次のとおりです。フィードバックをユーザーに通知するように特定の時刻に通知を設定する方法

MainActivity:

package com.myapp.priya.employeeapp; 

import android.app.AlarmManager; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v4.app.NotificationCompat; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RatingBar; 
import android.widget.Toast; 

import java.util.Calendar; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button btnLogin = (Button) findViewById(R.id.buttonLogin); 

     btnLogin.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(MainActivity.this,RatingActivity.class); 
       startActivity(intent); 
      } 
     }); 

     Calendar calendar = Calendar.getInstance(); 
     calendar.set(Calendar.HOUR_OF_DAY, 19); 
     calendar.set(Calendar.MINUTE, 14); 
     calendar.set(Calendar.SECOND, 0); 

     Intent intentResult = new Intent(MainActivity.this,MyReceiver.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, 
       intentResult,PendingIntent.FLAG_UPDATE_CURRENT); 
     //PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT); 
     AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE); 
     am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent); 

    } 


} 

RatingBar: `

package com.myapp.priya.employeeapp; 

import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v4.app.ActivityCompat; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RatingBar; 
import android.widget.Toast; 

public class RatingActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_rating); 

     final RatingBar simpleRatingBar = (RatingBar) findViewById(R.id.ratingBar); 
     Button submitButton = (Button) findViewById(R.id.buttonSubmit); 
     submitButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // get values and then displayed in a toast 
       String totalStars = "Total Stars:: " + simpleRatingBar.getNumStars(); 
       String rating = "Rating :: " + String.valueOf(simpleRatingBar.getRating()); 
       Toast.makeText(getApplicationContext(), totalStars + "\n" + rating, Toast.LENGTH_LONG).show(); 
      } 
     }); 
    } 




} 
` 

MyReceiver:

package com.myapp.priya.employeeapp; 

import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v4.app.NotificationCompat; 

/** 
* Created by priya on 09-08-2017. 
*/ 

public class MyReceiver extends BroadcastReceiver { 
    int MID = 0; 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     long when = System.currentTimeMillis(); 
     NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     Intent notificationIntent = new Intent(context,RatingActivity.class); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
        /*.setSmallIcon(R.drawable.employee_opinion_surveys)*/ 
       .setAutoCancel(true).setWhen(when) 
       .setContentIntent(pendingIntent) 
       .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}) 
       .setContentTitle("HR Feedback") 
       .setContentText("Please provide the feedback"); 
     manager.notify(MID,builder.build()); 
     MID++; 
    } 
} 

マニフェストファイル:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.myapp.priya.employeeapp"> 
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/> 
    <uses-permission android:name="android.permission.WAKE_LOCK"/> 


    <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> 
     <activity android:name=".RatingActivity"></activity> 
     <receiver android:name="com.myapp.priya.employeeapp.MyReceiver"/> 
    </application> 

</manifest> 
+0

を発射アプリをインストールアンドロイドのバージョンは何ですか? –

+0

私のエミュレータはNexus 6 API 24 – Priyanka

+0

です。時間を2分に変更した場合(動作しているかどうかを確認するためにのみ、Worksの場合は20分待っているので長い時間です) –

答えて

0
NotificationEventReceiver.setupAlarm(getApplicationContext()); 

注:これはあなたの主な活動です。 カレンダークラスの使用は実際には正確ではありません。 onCreateでサービスを開始してから、受信者クラスで、システム時間を必要な(24時間形式の)時間と比較し、通知を発します。

public class NotificationEventReceiver extends WakefulBroadcastReceiver { 

    private static final String ACTION_START_NOTIFICATION_SERVICE = "ACTION_START_NOTIFICATION_SERVICE"; 
    private static final String ACTION_DELETE_NOTIFICATION = "ACTION_DELETE_NOTIFICATION"; 

    private static final int NOTIFICATIONS_INTERVAL_IN_HOURS = 3; 

    public static void setupAlarm(Context context) { 
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
     PendingIntent alarmIntent = getStartPendingIntent(context); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
       getTriggerAt(new Date()), 
       20800000, 
       alarmIntent); 
    } 

    public static void cancelAlarm(Context context) { 
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
     PendingIntent alarmIntent = getStartPendingIntent(context); 
     alarmManager.cancel(alarmIntent); 
    } 

    private static long getTriggerAt(Date now) { 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTime(now); 
     //calendar.add(Calendar.HOUR, NOTIFICATIONS_INTERVAL_IN_HOURS); 
     return calendar.getTimeInMillis(); 
    } 

    private static PendingIntent getStartPendingIntent(Context context) { 
     Intent intent = new Intent(context, NotificationEventReceiver.class); 
     intent.setAction(ACTION_START_NOTIFICATION_SERVICE); 
     return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    } 

    public static PendingIntent getDeleteIntent(Context context) { 
     Intent intent = new Intent(context, NotificationEventReceiver.class); 
     intent.setAction(ACTION_DELETE_NOTIFICATION); 
     return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     Intent serviceIntent = null; 
     if (ACTION_START_NOTIFICATION_SERVICE.equals(action)) { 
      Log.i(getClass().getSimpleName(), "onReceive from alarm, starting notification service"); 
      serviceIntent = NotificationIntentService.createIntentStartNotificationService(context); 
     } else if (ACTION_DELETE_NOTIFICATION.equals(action)) { 
      Log.i(getClass().getSimpleName(), "onReceive delete notification action, starting notification service to handle delete"); 
      serviceIntent = NotificationIntentService.createIntentDeleteNotification(context); 
     } 

     if (serviceIntent != null) { 
      // Start the service, keeping the device awake while it is launching. 
      startWakefulService(context, serviceIntent); 
     } 
    } 
} 

注:これはあなたのNotificationEventReceiverにあります。これにより、通知トリガーが繰り返されます。

public class NotificationIntentService extends IntentService { 

    private static final int NOTIFICATION_ID = 1; 
    private static final String ACTION_START = "ACTION_START"; 
    private static final String ACTION_DELETE = "ACTION_DELETE"; 
    private Intent mainIntent; 

    public NotificationIntentService() { 
     super(NotificationIntentService.class.getSimpleName()); 
    } 

    public static Intent createIntentStartNotificationService(Context context) { 
     Intent intent = new Intent(context, NotificationIntentService.class); 
     intent.setAction(ACTION_START); 
     return intent; 
    } 

    public static Intent createIntentDeleteNotification(Context context) { 
     Intent intent = new Intent(context, NotificationIntentService.class); 
     intent.setAction(ACTION_DELETE); 
     return intent; 
    } 

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) 
    @Override 
    protected void onHandleIntent(Intent intent) { 
     Log.d(getClass().getSimpleName(), "onHandleIntent, started handling a notification event"); 
     try { 
      String action = intent.getAction(); 
      if (ACTION_START.equals(action)) { 
       String timeStamp = new SimpleDateFormat("HH").format(new Date()); 
       //comparing with required time 

       Log.d("noti", "processStartNotification: --"+timeStamp); 

       int notableTime = Integer.parseInt(timeStamp); 
      //if time matches then fire the method 
       if(notableTime>7){ 

         processStartNotification(); 

        }else 
         { 
          Log.d("noti", "processStartNotification: is halt state > 22" + timeStamp); 

         } 

      } 
     } finally { 
      WakefulBroadcastReceiver.completeWakefulIntent(intent); 
     } 
    } 

    private void processDeleteNotification(Intent intent) { 
     // Log something? 
    } 

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) 
    private void processStartNotification() { 
     int ran=new Random().nextInt(4); 

     StudentSavedPreferences studentSavedPreferences = new StudentSavedPreferences(getApplicationContext()); 
     String Student_id = studentSavedPreferences.getStudentId(); 
     final Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     int r[]={R.drawable.drip_otification_one,R.drawable.drip_notifications_two,R.drawable.drip_notifications_three,R.drawable.drip_notifications_four}; 
     String subtext[]={"Give MCQ type test to know about your skill and check your rank.","Watch Video lectures from highly qualified professionals","Get yourself updated by getting daily notifications","Learn even more by all new Audio books and Epub"}; 
     String from[]={"test","video","notification","epub"}; 
     if (Student_id == null) { 
      mainIntent = new Intent(getApplicationContext(), Splashh.class); 
        mainIntent.putExtra("from", "notificationDemo"); 
        mainIntent.putExtra("to", from[ran]); 

     } else { 
      mainIntent = new Intent(getApplicationContext(), MainDrawerActivity.class); 
        mainIntent.putExtra("from", "notificationDemo"); 
        mainIntent.putExtra("to", from[ran]); 
     } 
//  Intent mainIntent = new Intent(this, Splashh.class); 
//  mainIntent.putExtra("SUBJECT", "MHT-CET"); 
//  mainIntent.putExtra("Position","0"); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 
       NOTIFICATION_ID, 
       mainIntent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 
     // Do something. For example, fetch fresh data from backend to create a rich notification? 
     final Bitmap remote_picture_test = BitmapFactory.decodeResource(getApplicationContext().getResources(), r[ran]); 
     final Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.icn_app_icon); 
     android.app.Notification notif = new android.app.Notification.Builder(getApplicationContext()) 
       .setContentTitle("M-Educate welcomes you the Digital era of learning") 
       .setSubText(subtext[ran]) 
       .setSmallIcon(R.drawable.icn_app_icon) 
       .setLargeIcon(icon) 
       .setStyle(new android.app.Notification.BigPictureStyle() 
         .bigPicture(remote_picture_test)) 
       .setContentIntent(pendingIntent) 
       .setSound(alarmSound) 
       .build(); 
     NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0, notif); 

//  final NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
//  builder.setContentTitle("Scheduled Notification") 
//    .setAutoCancel(true) 
//    .setColor(getResources().getColor(R.color.actionbarbackground)) 
//    .setContentText("This notification has been triggered by Notification Service") 
//    .setSmallIcon(R.drawable.icn_app_icon); 
// 
// 

//  builder.setContentIntent(pendingIntent); 
//  builder.setDeleteIntent(NotificationEventReceiver.getDeleteIntent(this)); 
// 
//  final NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 
//  manager.notify(NOTIFICATION_ID, builder.build()); 
    } 
} 

注:必要な時間とシステム時間を比較し、その後、私は方法

+0

申し訳ありませんが、私は取得していません.. – Priyanka

+0

スニペットを送信するのを待つ –

+0

確かに..ありがとう.. – Priyanka

関連する問題