I want show telephone number when call ringing in Notification
, but I don't know how can I show Telephone number in Notification
!
For Telephone Number i BroadcastReceiver
and i can show this number in RecyclerView
, but i want show number in Notification
!
My Receiver codes:
public class NumberReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
DBHelper dbHelper = new DBHelper(context);
SQLiteDatabase database = dbHelper.getWritableDatabase();
dbHelper.saveNumber(number, database);
database.close();
}
Intent intent1 = new Intent(DBContract.UPDATE_UI_INTENT_FILTER);
context.sendBroadcast(intent1);
}
}
MainActivity codes:
public class Prabeesh_page extends AppCompatActivity {
private RecyclerView Prabeesh_LIST;
private TextView Prabeesh_TEXT;
private Adapter mAdapter;
private ArrayList<IncomingModel> list = new ArrayList<>();
private BroadcastReceiver broadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prabeesh_page);
Prabeesh_LIST = (RecyclerView) findViewById(R.id.prabeesh_list);
Prabeesh_TEXT = (TextView) findViewById(R.id.prabessh_textView);
Prabeesh_LIST.setLayoutManager(new LinearLayoutManager(this));
Prabeesh_LIST.setHasFixedSize(true);
mAdapter = new Adapter(list);
Prabeesh_LIST.setAdapter(mAdapter);
readFromDB();
addNotification();
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
readFromDB();
}
};
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(broadcastReceiver, new IntentFilter(DBContract.UPDATE_UI_INTENT_FILTER));
readFromDB();
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
}
public void readFromDB() {
list.clear();
DBHelper dbHelper = new DBHelper(this);
SQLiteDatabase database = dbHelper.getWritableDatabase();
Cursor cursor = dbHelper.readNumber(database);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String number;
int id;
id = cursor.getInt(cursor.getColumnIndex("ID"));
number = cursor.getString(cursor.getColumnIndex(DBContract.INCOMING_NUMBER));
list.add(new IncomingModel(id, number));
}
cursor.close();
dbHelper.close();
mAdapter.notifyDataSetChanged();
Prabeesh_LIST.setVisibility(View.VISIBLE);
Prabeesh_TEXT.setVisibility(View.GONE);
}
}
private void addNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification");
Intent notificationIntent = new Intent(this, Prabeesh_page.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
}
**With this codes i use numbers in `RecyclerView` :**
ublic class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
private ArrayList<IncomingModel> list = new ArrayList<>();
public Adapter(ArrayList<IncomingModel> list) {
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.prabbesh_row, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.ID.setText(Integer.toString(list.get(position).getId()));
holder.NUMBER.setText(list.get(position).getNumber());
}
@Override
public int getItemCount() {
return list.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private TextView ID, NUMBER;
public MyViewHolder(View itemView) {
super(itemView);
ID = (TextView) itemView.findViewById(R.id.prabeeshRow_ID);
NUMBER = (TextView) itemView.findViewById(R.id.prabeeshRow_NUMBER);
}
}
}
How can i show this number, in Notification
?
public class NumberReceiver extends BroadcastReceiver {
Context context;
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
addNotification(number)
DBHelper dbHelper = new DBHelper(context);
SQLiteDatabase database = dbHelper.getWritableDatabase();
dbHelper.saveNumber(number, database);
database.close();
}
Intent intent1 = new Intent(DBContract.UPDATE_UI_INTENT_FILTER);
context.sendBroadcast(intent1);
}
private void addNotification(String number) {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Notifications Example")
.setContentText(number);
Intent notificationIntent = new Intent(context, Prabeesh_page.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
}
I just added the addNotification method with a String parameter, and then called it from the onReceive method passing the number from the incoming call.