Я получаю номер телефона во время кольцевого состояния, но иногда он устанавливается в NULL во время состояния выкл. Я не могу поймать момент, когда он переходит к NULL. После этого, когда я беру звонок, он переходит в состояние Offhook , и я получил null в Callno, поэтому он дает мне nullpointerexception . < /p>
Как предотвратить эту ситуацию?private static boolean noCallListenerYet = true;
TelephonyManager telephonyManager;
static MyPhoneStateListener phoneListener;
private static Context context1;
Context context;
private int prevState;
String userId;
String incoming_number = null;
Bundle bundle;
String state;
private static String callno = null;
static SharedPreferences pref;
static int cidvalue;
/*Added to resolve the below bug:
* Bug: At the time of call comes on poped up note and
* below note was not send and new userid not
* replace with older userid.
*/
private static boolean isOnReceive = false;
public static String getCallno() {
return callno;
}
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
isOnReceive = true;
if( CallTrackerModel.isRecording() ){
}else{
CallTrackerModel.setCallId("");
try{
if (intent.getAction()
.equals("android.intent.action.NEW_OUTGOING_CALL")) {
if ((bundle = intent.getExtras()) != null) {
callno = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
callno = callno.replaceAll(" ", "");
}
}
}
catch(Exception e){
}
try{
if (noCallListenerYet) {
telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (phoneListener == null) {
phoneListener = new MyPhoneStateListener(context);
telephonyManager.listen(phoneListener,
PhoneStateListener.LISTEN_CALL_STATE);
}
noCallListenerYet = false;
}
}catch(Exception e){
isOnReceive = false;
}
context1 = context;
}
}
public static int returncid() {
int cid;
pref = context1.getSharedPreferences("Myprefer", 0);
SharedPreferences.Editor editor = pref.edit();
cid = pref.getInt("currentcid", 0);
if (cid == 0) {
cid = cid + 1;
}
editor.putInt("currentcid", cid);
editor.commit();
pref = context1.getSharedPreferences("Myprefer", 0);
cidvalue = pref.getInt("currentcid", 0);
return cidvalue;
}
private class MyPhoneStateListener extends PhoneStateListener {
Context context;
MyPhoneStateListener(Context c) {
super();
context = c;
}
/**
* Listen call state changes.
*/
public void onCallStateChanged(int state, String incomingNumber) {
CallTrackerModel ctm = new CallTrackerModel(context1);
switch (state) {
// Incoming/Outgoing call over.
case TelephonyManager.CALL_STATE_IDLE:
if (CallTrackerModel.returnRecordStarted()) {
ctm.stopRecording();
userId = RetrieveUserId.getUserId();
}
//For Received calls.
if (prevState == TelephonyManager.CALL_STATE_OFFHOOK) {
try{
cidvalue = pref.getInt("currentcid", 0);
++cidvalue;
pref = context1.getSharedPreferences("Myprefer", 0);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("currentcid", cidvalue);
editor.commit();
prevState = state;
// Start note activity.
Intent i = new Intent(context1, NoteActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (userId == null) {
userId = "@@";
}
i.putExtra("userId", userId);
i.putExtra("isSend", false);
i.putExtra("incomingNumber", incoming_number);
context1.startActivity(i);
i = null;
}catch(Exception ex){
}
}
//For missed calls.
if(prevState==TelephonyManager.CALL_STATE_RINGING){
prevState=state;
}
break;
//If the caller or receiver picks up the phone
case TelephonyManager.CALL_STATE_OFFHOOK:
try{
if( CallTrackerModel.isRecording() ){
break;
}
if( NoteActivity.getIsStart() ){
NoteActivity.setStop(true);
}
prevState = state;
if (callno.length() == 13) {
incoming_number = callno.substring(3);
} else if (callno.length() == 11) {
incoming_number = callno.substring(1);
} else {
incoming_number = callno;
}
}catch(Exception ex){
isOnReceive = false;
}
try{
if( NoteActivity.getIsStop() ){
if(NoteActivity.getLater()){
NoteActivity.setLater(false);
NoteActivity.setStop(false);
}else{
NoteActivity.later();
}
}
}catch(Exception e){
isOnReceive = false;
}
try{
Intent i = new Intent(context1, RetrieveUserId.class);
i.putExtra("incoming number", incoming_number);
context1.startService(i);
// start recording
ctm.startRecording();
}catch(Exception e){
isOnReceive = false;
}
break;
case TelephonyManager.CALL_STATE_RINGING:
if( CallTrackerModel.isRecording() ){
}else{
prevState = state;
callno = incomingNumber;
callno = callno.replaceAll(" ", "");
}
break;
}
}
}
< /code>
} < /p>
Подробнее здесь: https://stackoverflow.com/questions/396 ... onymanager
Номер телефона установлен на NULL во время Off D ⇐ Android
Форум для тех, кто программирует под Android
-
Anonymous
1759640857
Anonymous
Я получаю номер телефона во время кольцевого состояния, но иногда он устанавливается в NULL во время состояния выкл. Я не могу поймать момент, когда он переходит к NULL. После этого, когда я беру звонок, он переходит в состояние Offhook , и я получил null в Callno, поэтому он дает мне nullpointerexception . < /p>
Как предотвратить эту ситуацию?private static boolean noCallListenerYet = true;
TelephonyManager telephonyManager;
static MyPhoneStateListener phoneListener;
private static Context context1;
Context context;
private int prevState;
String userId;
String incoming_number = null;
Bundle bundle;
String state;
private static String callno = null;
static SharedPreferences pref;
static int cidvalue;
/*Added to resolve the below bug:
* Bug: At the time of call comes on poped up note and
* below note was not send and new userid not
* replace with older userid.
*/
private static boolean isOnReceive = false;
public static String getCallno() {
return callno;
}
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
isOnReceive = true;
if( CallTrackerModel.isRecording() ){
}else{
CallTrackerModel.setCallId("");
try{
if (intent.getAction()
.equals("android.intent.action.NEW_OUTGOING_CALL")) {
if ((bundle = intent.getExtras()) != null) {
callno = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
callno = callno.replaceAll(" ", "");
}
}
}
catch(Exception e){
}
try{
if (noCallListenerYet) {
telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (phoneListener == null) {
phoneListener = new MyPhoneStateListener(context);
telephonyManager.listen(phoneListener,
PhoneStateListener.LISTEN_CALL_STATE);
}
noCallListenerYet = false;
}
}catch(Exception e){
isOnReceive = false;
}
context1 = context;
}
}
public static int returncid() {
int cid;
pref = context1.getSharedPreferences("Myprefer", 0);
SharedPreferences.Editor editor = pref.edit();
cid = pref.getInt("currentcid", 0);
if (cid == 0) {
cid = cid + 1;
}
editor.putInt("currentcid", cid);
editor.commit();
pref = context1.getSharedPreferences("Myprefer", 0);
cidvalue = pref.getInt("currentcid", 0);
return cidvalue;
}
private class MyPhoneStateListener extends PhoneStateListener {
Context context;
MyPhoneStateListener(Context c) {
super();
context = c;
}
/**
* Listen call state changes.
*/
public void onCallStateChanged(int state, String incomingNumber) {
CallTrackerModel ctm = new CallTrackerModel(context1);
switch (state) {
// Incoming/Outgoing call over.
case TelephonyManager.CALL_STATE_IDLE:
if (CallTrackerModel.returnRecordStarted()) {
ctm.stopRecording();
userId = RetrieveUserId.getUserId();
}
//For Received calls.
if (prevState == TelephonyManager.CALL_STATE_OFFHOOK) {
try{
cidvalue = pref.getInt("currentcid", 0);
++cidvalue;
pref = context1.getSharedPreferences("Myprefer", 0);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("currentcid", cidvalue);
editor.commit();
prevState = state;
// Start note activity.
Intent i = new Intent(context1, NoteActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (userId == null) {
userId = "@@";
}
i.putExtra("userId", userId);
i.putExtra("isSend", false);
i.putExtra("incomingNumber", incoming_number);
context1.startActivity(i);
i = null;
}catch(Exception ex){
}
}
//For missed calls.
if(prevState==TelephonyManager.CALL_STATE_RINGING){
prevState=state;
}
break;
//If the caller or receiver picks up the phone
case TelephonyManager.CALL_STATE_OFFHOOK:
try{
if( CallTrackerModel.isRecording() ){
break;
}
if( NoteActivity.getIsStart() ){
NoteActivity.setStop(true);
}
prevState = state;
if (callno.length() == 13) {
incoming_number = callno.substring(3);
} else if (callno.length() == 11) {
incoming_number = callno.substring(1);
} else {
incoming_number = callno;
}
}catch(Exception ex){
isOnReceive = false;
}
try{
if( NoteActivity.getIsStop() ){
if(NoteActivity.getLater()){
NoteActivity.setLater(false);
NoteActivity.setStop(false);
}else{
NoteActivity.later();
}
}
}catch(Exception e){
isOnReceive = false;
}
try{
Intent i = new Intent(context1, RetrieveUserId.class);
i.putExtra("incoming number", incoming_number);
context1.startService(i);
// start recording
ctm.startRecording();
}catch(Exception e){
isOnReceive = false;
}
break;
case TelephonyManager.CALL_STATE_RINGING:
if( CallTrackerModel.isRecording() ){
}else{
prevState = state;
callno = incomingNumber;
callno = callno.replaceAll(" ", "");
}
break;
}
}
}
< /code>
} < /p>
Подробнее здесь: [url]https://stackoverflow.com/questions/39631794/phone-number-is-set-to-null-at-the-time-of-offhook-state-of-telephonymanager[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия