我正在创建一个应用程序和我的代码的某些部分,我需要在收到呼叫时创建通知。问题是我无法实现状态栏通知,因为我必须检查数据库中的数字,然后在通知中显示所需的数据。
这是我的代码:
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);
}
}
//这是清单文件:
<receiver android:name=".IncomingCall" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
//我也添加了电话状态权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
我明白我在某些地方做错了但我无法识别它... 请告诉我我错在哪里......
答案 0 :(得分:1)
这可能对某人有所帮助。这是实施:
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);
}
}
确保您已将接收器添加到Manifest File,如下所示:
<receiver android:name=".IncomingCall" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>