1

Topic: Call reception

import android.support.v7.app. AppCompatActivity;
import android.os. Bundle;
import android.telephony. PhoneStateListener;
import android.telephony. TelephonyManager;
import android.widget. Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
TelephonyManager. [color=red] listen [/color] (stateListener, PhoneStateListener. LISTEN_CALL_STATE);
}
}[color=red] ~ [/color]
PhoneStateListener stateListener = new PhoneStateListener () {
public void onCallStateChanged (int state, String incomingNumber) {
switch (state) {
case TelephonyManager. CALL_STATE_IDLE: break;
case TelephonyManager. CALL_STATE_OFFHOOK: break;
case TelephonyManager. CALL_STATE_RINGING:
Toast.makeText (getApplicationContext (), incomingNumber, Toast. LENGTH_LONG).show ();
try {
} catch (Exception e) {
e.printStackTrace ();
}
break;
}
}
};[color=red] ~ [/color]

Writes Non-static method listen cannot be referenced from a static context
Where static to write?

2

Re: Call reception

With brackets understood. Remained listen

3

Re: Call reception

For example so: https://stackoverflow.com/questions/133 … telistener

4

Re: Call reception

wadman, I as understood threatened on an elephant. Decided to take an elephant to pieces.
1.) service which lives after an output from application turned out.
2.) service which lives after smart phone reboot while is not present.

public class BootBroadcast extends BroadcastReceiver {
@Override
public void onReceive (Context context, Intent intent) {
context.startService (new Intent (context, MyService.class));
}
}

I will not understand how to launch BootBroadcast (registered in , as BOOT_COMPLETED) which launches Service
3.) in service to implement the listener of a state of the smart phone and to pick out entering number
Or I created Broadcasting receiver BootBroadcast which launches Service after reboot?

5

Re: Call reception

JavaStudent wrote:

registered in , as BOOT_COMPLETED

It is Somehow difficult to guess on a coffee thick...

6

Re: Call reception

<activity android:name = ". MainActivity">
<intent-filter>
<action android:name = "android.intent.action. MAIN"/>
<category android:name = "android.intent.category. LAUNCHER"/>
</intent-filter>
<receiver android:name = ". BootBroadcast">
<intent-filter>
<action android:name = "android.intent.action. BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</activity>
<service
android:name = ". MyService"
android:enabled = "true"
android:exported = "false">
</service>

I faced other task. At smart phone reboot, it is necessary somehow still to MyService it will be connected... And in the message notification message to shower, the pier service works.

7

Re: Call reception

Here esteem about services https://www.google.com/search?q=startandroid startservice

8

Re: Call reception

wadman, I read there and here, everywhere where comes across.
I correctly understood that I need to make:
1) Service which is twisted in the smart phone and waits for a signal on working off
2) the Broadcasting receiver in service which waits a call and launches working off
3) Sending POST of request from service when the Broadcasting receiver found out a call
4) Determination of number of a call
5) the Broadcasting receiver in service which launches service after smart phone reboot
6) Application which launches, and stops service.
I will try to make on pieces. While my service takes off, when from it I send POST request.

9

Re: Call reception

The task is not clear meanwhile.
But basically it is described truly: the receiver  starts the listener of calls. Application too can launch the listener. The listener can already launch the services necessary to it on events to launch.

10

Re: Call reception

JavaStudent wrote:

Writes Non-static method listen cannot be referenced from a static context
Where static to write?

1. To create object TelephonyManager tm;
2.  its methods.
3. Reception of entering calls by indirect applications is blocked, since Android 6 "with a view of safety". To 6 versions it is possible.

11

Re: Call reception

wadman, the Problem simple. I wrote the program in which it is necessary to register requests including to enter telephone numbers.
To type to clients, employees, to write  ... Still them 3 phone numbers. To look at this employee terribly.
2 sets in 2 ears.... I Want to try to make so that the employee did not take the smart phone in hands...... And number did not write off with
The smart phone. To a dial-up of number me it is still far, and here to transfer number of an entering call to basis and to process it, I as understand to a smog.
With service understood. I try to palm off instead of simple service here this.
It is service

public class ServiceReceiver extends BroadcastReceiver {
TelephonyManager telephony;
public void onReceive (Context context, Intent intent) {
MyPhoneStateListener phoneListener = new MyPhoneStateListener ();
telephony = (TelephonyManager) context.getSystemService (Context. TELEPHONY_SERVICE);
telephony.listen (phoneListener, PhoneStateListener. LISTEN_CALL_STATE);
}
public void onDestroy () {
telephony.listen (null, PhoneStateListener. LISTEN_NONE);
}
}

It is a class

import android.telephony. PhoneStateListener;
import android.telephony. TelephonyManager;
import android.util.Log;
public class MyPhoneStateListener extends PhoneStateListener {
final String LOG = "myLog";
public void onCallStateChanged (int state, String incomingNumber) {
switch (state) {
case TelephonyManager. CALL_STATE_IDLE:
Log.d (LOG, "IDLE");
break;
case TelephonyManager. CALL_STATE_OFFHOOK:
Log.d (LOG, "OFFHOOK");
break;
case TelephonyManager. CALL_STATE_RINGING:
Log.d (LOG, "RINGING" + incomingNumber);
break;
}
}
}

MainActivity whence I launch service of 2 buttons...

public class MainActivity extends AppCompatActivity {
final String LOG = "myLog";
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
}
public void onClickStart (View v) {
Log.d (LOG, "onClickStart");
startService (new Intent (this, ServiceReceiver.class));
}
public void onClickStop (View v) {
Log.d (LOG, "onClickStop");
stopService (new Intent (this, ServiceReceiver.class));
}
public class BootBroadcast extends BroadcastReceiver {
@Override
public void onReceive (Context context, Intent intent) {
context.startService (new Intent (context, ServiceReceiver.class));
}
}
}

While I understand nothing. I understand.

12

Re: Call reception

Found a jamb. I try to palm off instead of service . It is necessary to launch from service .... And to listen to it.
Error found out in the manifesto when tried  to name service.

13

Re: Call reception

I try to declare in service of the listener

public int onStartCommand (Intent intent, int flags, int startId)
{
IntentFilter filter = new IntentFilter ();
filter.addAction (TelephonyManager. CALL_STATE_RINGING);
registerReceiver (ServiceReceiver, filter);
return super.onStartCommand (intent, flags, startId);
}

Errors climb.

14

Re: Call reception

Errors are not present. But the listener I do not know how to check up.
That listens? Where listens?

public class MyService extends Service {
final private String LOG = "myLog";
private static TelephonyManager telephony;
public void onCreate () {
super.onCreate ();
final BroadcastReceiver ServiceReceiver = new BroadcastReceiver ()
{
public void onReceive (Context context, Intent intent)
{
MyPhoneStateListener phoneListener = new MyPhoneStateListener ();
telephony = (TelephonyManager) context.getSystemService (Context. TELEPHONY_SERVICE);
telephony.listen (phoneListener, PhoneStateListener. LISTEN_CALL_STATE);
}
public void onDestroy () {
telephony.listen (null, PhoneStateListener. LISTEN_NONE);
}
};
IntentFilter filter = new IntentFilter ();
filter.addAction (READ_PHONE_STATE);
registerReceiver (ServiceReceiver, filter);
}

Prompt where to dig? My lines:

IntentFilter filter = new IntentFilter ();
filter.addAction (READ_PHONE_STATE);
registerReceiver (ServiceReceiver, filter);

Can here that not so?