我想在Notification
中来电铃声时显示电话号码,但我不知道如何显示电话号码在Notification
!
对于我BroadcastReceiver
的电话号码,我可以在RecyclerView
中显示此号码,但我想在Notification
中显示号码!
我的收件人代码:
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);
}
}
主要活动代码:
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);
}
}
}
如何在Notification
中显示此号码?
答案 0 :(得分:0)
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());
}
}
我刚刚使用String参数添加了addNotification方法,然后从onReceive方法调用它来传递来电中的号码。