问题描述
我创建一个应用程序,我的code的某一部分,我需要的时候我接到一个电话,以创建一个通知。问题是,我不能够实现的状态栏通知,因为我要检查在数据库中的编号,然后在通知显示所需的数据。
I am creating an application and a certain part of my code I need to create a notification when I receive a call. The problem is that I am not able to implement the status bar notification, since I have to check the number in the database and then show the required data in Notification.
下面是我的code:
public class IncomingCall extends BroadcastReceiver {
private Context mContext = null;
NotificationManager NM;
public String msg;
public void onReceive(Context context, Intent intent) {
mContext = context;
try {
// TELEPHONY MANAGER class object to register one listner
TelephonyManager tmgr = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
// Create Listner
MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
// Register listener for LISTEN_CALL_STATE
tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
Log.e("Phone Receive Error", " " + e);
}
}
private class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
Log.d("MyPhoneListener", state + " incoming no:" + incomingNumber);
if (state == 1) {
msg = "New Phone Call Event. Incoming Number : "
+ incomingNumber;
int duration = Toast.LENGTH_LONG;
notify(); // I am Getting the Error Here
Toast toast = Toast.makeText(mContext, msg, duration);
toast.show();
}
}
}
public void notify(View vobj)
{
String title = "TEST";
String subject = "THIS IS TEST DATA";
NM = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification(
android.R.drawable.stat_notify_more, title,
System.currentTimeMillis());
PendingIntent pending = PendingIntent.getActivity(mContext, 0,
new Intent(), 0);
notify.setLatestEventInfo(mContext, subject, msg, pending);
NM.notify(0, notify);
}
}
//这里是清单文件:
// Here is the Manifest File :
<receiver android:name=".IncomingCall" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
//我已经加入了手机状态权限,以及:
// I have added the Phone State Permission as well :
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
我知道我做错了一些地方,但我不能承认它...
请告诉我在哪里,我错了......
I understand that I am doing wrong some where but I am not able to recognize it...Please Tell me where I am wrong...
推荐答案
这可能会帮助一些之一。下面是执行:
This might help some one. Here is the Implementation :
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
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;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
@SuppressLint("NewApi")
public class IncomingCall extends BroadcastReceiver {
private Context mContext = null;
NotificationManager NM;
public String msg;
public void onReceive(Context context, Intent intent) {
mContext = context;
try {
// TELEPHONY MANAGER class object to register one listner
TelephonyManager tmgr = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
// Create Listner
MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
// Register listener for LISTEN_CALL_STATE
tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
Log.e("Phone Receive Error", " " + e);
}
}
private class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
Log.d("MyPhoneListener", state + " incoming no:" + incomingNumber);
if (state == 1) {
msg = incomingNumber;
createNotification();
}
}
}
@SuppressLint("NewApi")
public void createNotification() {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(mContext, NotificationImplementActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(mContext, 0, intent,
0);
// Build notification
// Actions are just fake
Notification noti = new NotificationCompat.Builder(mContext)
.setContentTitle("New Call from " + msg)
.setContentText("Details :")
.setSmallIcon(R.drawable.logo).setContentIntent(pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) mContext
.getSystemService(Activity.NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
}
请确保您所添加的接收器清单文件如下:
Make Sure you have added the receiver to Manifest File as follows :
<receiver android:name=".IncomingCall" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
这篇关于从我的应用程序显示状态栏通知时呼叫与来电号码接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!