Guest User

Untitled

a guest
Feb 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. package com.notify;
  2.  
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.app.Service;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.os.IBinder;
  10. import android.telephony.PhoneStateListener;
  11. import android.telephony.TelephonyManager;
  12. import android.widget.Toast;
  13.  
  14. public class NotifyService extends Service
  15. {
  16.    
  17.     private static final int HELLO_ID = 1;
  18.     private TelephonyManager tlfMan;
  19.    
  20.    
  21.     private PhoneStateListener phStateListener = new PhoneStateListener() {
  22.         String number;
  23.        
  24.         @Override
  25.         public void onCallStateChanged(int state, String incomingNumber)
  26.         {
  27.             super.onCallStateChanged(state, incomingNumber);
  28.             number = incomingNumber;
  29.             if(state == TelephonyManager.CALL_STATE_RINGING)
  30.                 calling();
  31.         }
  32.     };
  33.    
  34.                  
  35.    private void calling()
  36.    {
  37.         String ns = Context.NOTIFICATION_SERVICE;
  38.         NotificationManager myNotificationManager = (NotificationManager) getSystemService(ns);
  39.        
  40.         int icon = R.drawable.android_icon;
  41.         CharSequence tickerText = "Hello, you've been notified! ";
  42.         long when = System.currentTimeMillis();
  43.  
  44.         Notification ntf = new Notification(icon, tickerText, when);
  45.          
  46.         Context ctx = getApplicationContext();
  47.         CharSequence contentTitle = "Missed call";
  48.         CharSequence contentText = "From ";
  49.         Intent notificationIntent = new Intent(this, ManageNumber.class);
  50.         PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
  51.  
  52.         ntf.setLatestEventInfo(ctx, contentTitle, contentText, contentIntent);
  53.        
  54.         myNotificationManager.notify(HELLO_ID, ntf);
  55.    }
  56.    
  57.     @Override
  58.     public void onCreate()
  59.     {
  60.         super.onCreate();
  61.        
  62.         tlfMan = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
  63.         tlfMan.listen(phStateListener, PhoneStateListener.LISTEN_CALL_STATE);
  64.     }
  65.     @Override
  66.     public void onDestroy()
  67.     {
  68.         super.onDestroy();
  69.        
  70.         Toast.makeText(this, "Service.onDestroy()", Toast.LENGTH_LONG).show();
  71.         tlfMan.listen(phStateListener, PhoneStateListener.LISTEN_NONE);
  72.     }
  73.  
  74.     @Override
  75.     public void onStart(Intent i, int startId)
  76.     {
  77.         super.onStart(i, startId);
  78.        
  79.         i.getStringExtra("number");
  80.         Toast.makeText(this, "hei onStart()", Toast.LENGTH_LONG).show();   
  81.     }
  82.  
  83.     @Override
  84.     public IBinder onBind(Intent i)
  85.     {
  86.         Toast.makeText(this, "Service.onBind()", Toast.LENGTH_LONG).show();
  87.         return null;
  88.     }
  89. }
Add Comment
Please, Sign In to add comment