Here,in the OutgoingReceiver its inner onReceive method is not invoking when the class is called.
but when i am calling OutgoingReciver class it is only calling its constructor. not able to call method inside it.
Can connection is established of outgoing call.
How to invoke the class myListener so that it can tell that call is in which state like onCalling, onCallEstablished.
Its manifest file is:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<receiver android:name=".OutgoingReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
class OutgoingRecevier:-
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.sip.SipAudioCall;
import android.util.Log;
public class OutgoingReceiver extends BroadcastReceiver
{
public OutgoingReceiver()
{
Log.d("outgoing","in outgoing listener");
//this is been called
}
// but not invoking this onRecevie method. Were i am wrong??
#Override
public void onReceive(Context context, Intent intent)
{
Log.d("onRecevice","hi");
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
SipAudioCall.Listener listener = new myListener(context);
}
}
myListener class:-
import android.content.Context;
import android.media.MediaPlayer;
import android.net.sip.SipAudioCall;
import android.net.sip.SipException;
import android.net.sip.SipProfile;
import android.util.Log;
class myListener extends SipAudioCall.Listener {
private Context context;
public myListener(Context context)
{
Log.d("mylistener","i am in");
this.context = context;
}
#Override
public void onRinging(SipAudioCall call, SipProfile caller) {
try {
Log.d("inRinging","in");
call.answerCall(30);
} catch (SipException e) {
e.printStackTrace();
}
}
#Override
public void onReadyToCall(SipAudioCall call) {
Log.d("ReadyCall","IncomingCallReceiver.java onReadyToCall : " + call.toString());
}
#Override
public void onCalling(SipAudioCall call) {
Log.d("Calling","IncomingCallReceiver.java onCalling : " + call.toString());
}
#Override
public void onRingingBack(SipAudioCall call) {
Log.d("RingingBack","IncomingCallReceiver.java onRingingBack : " + call.toString());
}
#Override
public void onCallEstablished(SipAudioCall call) {
Log.d("CallEstablished","IncomingCallReceiver.java onCallEstablished : " + call.toString());
if (call.isInCall()) {
Log.d("CallEstablished","IncomingCallReceiver.java isInCall : " + call.toString());
}
if (call.isOnHold()) {
Log.d("CallEstablished","IncomingCallReceiver.java isOnHold : " + call.toString());
}
if (call.isMuted()) {
Log.d("CallEstablished","IncomingCallReceiver.java isMuted : " + call.toString());
}
call.startAudio();
}
#Override
public void onCallEnded(SipAudioCall call) {
Log.d("CallEnded","IncomingCallReceiver.java onCallEnded : " + call.toString());
}
#Override
public void onCallBusy(SipAudioCall call) {
Log.d("CallBusy","IncomingCallReceiver.java onCallBusy : " + call.toString());
}
#Override
public void onCallHeld(SipAudioCall call) {
Log.d("CallHeld","IncomingCallReceiver.java onCallHeld : " + call.toString());
}
#Override
public void onError(SipAudioCall call, int errorCode, String errorMessage) {
Log.d("CallError","IncomingCallReceiver.java IncomingCallReceiver.java onError : " + call.toString() + "; errorCode: " + errorCode + "; errorMessage: " + errorMessage);
}
#Override
public void onChanged(SipAudioCall call) {
Log.d("CallChanged","IncomingCallReceiver.java onReadyToCall : " + call.toString());
}
}
Mainactivity :-
.
. //code
.
if(TelephonyManager.CALL_STATE_OFFHOOK == state)
{
Log.i("endcallListener hook", "OFFHOOK");
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
//called here
BroadcastReceiver br = new OutgoingReceiver();
getApplicationContext().registerReceiver(br,intentFilter);
}
.
. //code
.
output:-
I/endcallListener hook: OFFHOOK
D/outgoing: in outgoing listener
//terminated
thanks in advance.
If you want to capture an outgoing call by using TelephonyManager then do this:
TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
PhoneStateListener listener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
super.onCallStateChanged(state, incomingNumber);
switch(state) {
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if(incomingNumber==null) {
//outgoing call
} else {
//incoming call
}
break;
case TelephonyManager.CALL_STATE_RINGING:
if(incomingNumber==null) {
//outgoing call
} else {
//incoming call
}
break;
}
}
};
Related
What should I do to receive SIP calls even when app is in background, exits or restarts?
Currently we're running a foreground service with sticky notification to keep connected with our sip server. This solution isn't completely working
Please help me to achieve this, Thanks in advance!
Firstly, you want a Mainservice that never stops (Even when the device booted, with the help of a bootup receiver).
You have to initialise your sip manager, sip profile and call related codes in the MainService and not in the MainActivity.
Then, You need a BroadCast receiver that receives incoming sip calls. see below example,
public class YourReceiver extends BroadcastReceiver {
SipAudioCall incomingCall = null;
private static YourReceiver instance;
MainService mainService;
public Intent incomingCallIntent;
public static YourReceiver getInstance() {
return instance;
}
/**
* Processes the incoming call, answers it, and hands it over to the
* MainActivity.
* #param context The context under which the receiver is running.
* #param intent The intent being received.
*/
#Override
public void onReceive(final Context context, Intent intent) {
Log.i(TAG, "onReceive: ");
instance = this;
mainService = MainService.getInstance();
try {
SipAudioCall.Listener listener = new SipAudioCall.Listener() {
#Override
public void onRinging(SipAudioCall call, SipProfile caller) {
Log.i(TAG, "onRinging: ");
}
// extra added
#Override
public void onRingingBack(SipAudioCall call) {
Log.i(TAG, "onRingingBack: ");
super.onRingingBack(call);
}
#Override
public void onReadyToCall(SipAudioCall call) {
Log.i(TAG, "onReadyToCall: ");
super.onReadyToCall(call);
}
#Override
public void onError(SipAudioCall call, int errorCode, String errorMessage) {
Log.e(TAG, "onError: errorCode = " + errorCode + ", errorMessage = " + errorMessage);
super.onError(call, errorCode, errorMessage);
}
#Override
public void onChanged(SipAudioCall call) {
Log.i(TAG, "onChanged: ");
super.onChanged(call);
}
#Override
public void onCalling(SipAudioCall call) {
Log.i(TAG, "onCalling: ");
super.onCalling(call);
}
#Override
public void onCallHeld(SipAudioCall call) {
Log.i(TAG, "onCallHeld: ");
super.onCallHeld(call);
}
#Override
public void onCallBusy(SipAudioCall call) {
Log.i(TAG, "onCallBusy: ");
super.onCallBusy(call);
}
#Override
public void onCallEnded(SipAudioCall call) {
Log.i(TAG, "onCallEnded: ");
}
#Override
public void onCallEstablished(SipAudioCall call) {
Log.i(TAG, "onCallEstablished: ");
super.onCallEstablished(call);
}
};
mainService = MainService.getInstance();
incomingCall = mainService.manager.takeAudioCall(intent,listener);
if(mainService.manager.isIncomingCallIntent(intent)){
incomingCallIntent = intent;
}
//starting call screen activity when the receiver receives incoming call
Intent i = new Intent(context, CallActivity.class);
i.putExtra("name", peerName);
i.putExtra("number", peerNumber);
i.putExtra("callType", "Incoming");
context.startActivity(i);
//Sip call received by YourReceiver is assigned to MainService.call so that the MainService can do the further processes.
mainService.call = incomingCall;
} catch (Exception e) {
if (incomingCall != null) {
incomingCall.close();
}
}
}
}
For making a Service run continuously, start a Job scheduler to make this job be a part of android system jobs.
package com.xxx;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import androidx.annotation.RequiresApi;
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class XXJobService extends JobService {
private String TAG = "XXJobService";
private Context context;
public XXJobService() {
}
#Override
public boolean onStartJob(JobParameters params) {
context = this;
try {
if (!MyApp.isServiceRunning(context, MainService.class.getName())) {
Log.i(TAG, "onStartJob : MainService not running so start");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.getApplicationContext()
.startForegroundService(new Intent(context, MainService.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
} else {
context.getApplicationContext()
.startService(new Intent(context, MainService.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
}
} catch (Exception e) {
Log.e(TAG, "Exception - MainService not running: " + e.getLocalizedMessage());
}
Log.i(TAG, "onStartJob, returnValue = " + returnValue);
return returnValue;
}
#Override
public boolean onStopJob(JobParameters params) {
boolean returnValue = true;
Log.i(TAG, "onStopJob, returnValue = " + returnValue);
return returnValue;
}
}
Create an Application class in your project that extends Application and define it in your manifest also.
import android.app.ActivityManager;
import android.app.Application;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import java.util.Arrays;
import java.util.List;
public class MyApp extends Application {
Context context;
private static MyApp instance;
static String TAG = "MyApp";
MainService mainService;
JobScheduler jobScheduler;
private static final int JOB_ID = 1;
public static MyApp getInstance() {
return instance;
}
#Override
public void onCreate() {
super.onCreate();
context = this;
instance = this;
Log.i(TAG, "onCreate: ");
// Job scheduler for android version Lolipop(Android 5.0) and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
ComponentName jobService = new ComponentName(getPackageName(), XXJobService.class.getName());
Log.e(TAG, "onCreate: ComponentName : " + jobService );
JobInfo jobInfo = new JobInfo.Builder(JOB_ID, jobService)
.setPersisted(true)
.setPeriodic(5000)
.build();
int jobId = jobScheduler.schedule(jobInfo);
if (jobId == JobScheduler.RESULT_SUCCESS) {
Log.e(TAG, "JobScheduler RESULT_SUCCESS");
// Toast.makeText(context, "Successfully scheduled job : " + jobId, Toast.LENGTH_SHORT).show();
} else {
Log.e(TAG, "JobScheduler RESULT_FAILURE: " + jobId);
// Toast.makeText(context, "RESULT_FAILURE: " + jobId, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.e(TAG, "JobScheduler Exception : " + e.getLocalizedMessage());
}
}
if (!isServiceRunning(context, MainService.class.getName())) {
try {
Intent intent = new Intent(context, MainService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
startService(intent);
}
} catch (Exception e) {
Log.e(TAG, "Exception startService : " + e.getLocalizedMessage());
}
}
mainService = MainService.getInstance();
Log.e(TAG," MainSerivice : " + mainService);
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
#Override
public void uncaughtException(Thread t, Throwable e) {
handleUncaughtException(t, e);
}
});
}
public static boolean isServiceRunning(Context context, String serviceClassName) {
if (context == null || serviceClassName.equalsIgnoreCase("")) {
Log.i(TAG, "isServiceRunning called with context = null or service name blank");
return false;
}
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
for (ActivityManager.RunningServiceInfo runningServiceInfo : services) {
if (runningServiceInfo.service.getClassName().equals(serviceClassName))
return true;
}
return false;
}
}
Finally, put a BootUpReceiver to automatically start the notofication and services whenever the phone is restarted. It should have the permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<receiver
android:name=".StartupReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.xx.MainService" />
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
Running a service is the right way. You just have to start the activity which will handle your incoming call from the service. The activity can then bind to your service and request all it needs to take over the call. That's actually all you need.
Hello I am new to Android Development. I have created an NSD Utility class. Currently its unable to call the method DiscoveryListner. Any help will be great
MainActivity
package com.example.android.implicitintents;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.example.android.implicitintents.Utils.NsdUtils;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "NsdChat";
public static String URL_WEB;
NsdUtils mNsdUtils;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NsdUtils mNsdUtils = new NsdUtils(this);
mNsdUtils.initializeNsd();
}
public void onClickOpenWebpageButton(View view) {
String urlAsString = "http://192.168.10.1";
openWebPage(urlAsString);
}
private void openWebPage(String url) {
Intent intent = new Intent(this, ConfigActivity.class);
intent.putExtra(URL_WEB, url);
Log.d(TAG, "Button Pressed");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
}
NSD Utility Class
package com.example.android.implicitintents.Utils;
import android.content.Context;
import android.net.nsd.NsdServiceInfo;
import android.net.nsd.NsdManager;
import android.util.Log;
public class NsdUtils {
Context mContext;
NsdManager mNsdManager;
NsdManager.ResolveListener mResolveListener;
NsdManager.DiscoveryListener mDiscoveryListener;
public static final String SERVICE_TYPE = "http";
public static final String TAG = "NsdHelper";
public String mServiceName = "Plug_Service";
NsdServiceInfo mService;
public NsdUtils(Context context) {
mContext = context;
mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
}
public void initializeNsd() {
initializeResolveListener();
initializeDiscoveryListener();
}
public void initializeDiscoveryListener() {
mDiscoveryListener = new NsdManager.DiscoveryListener() {
#Override
public void onDiscoveryStarted(String regType) {
Log.d(TAG, "Service discovery started");
}
#Override
public void onServiceFound(NsdServiceInfo service) {
Log.d(TAG, "Service discovery success" + service);
if (!service.getServiceType().equals(SERVICE_TYPE)) {
Log.d(TAG, "Unknown Service Type: " + service.getServiceType());
} else if (service.getServiceName().equals(mServiceName)) {
Log.d(TAG, "Same machine: " + mServiceName);
} else if (service.getServiceName().contains(mServiceName)){
mNsdManager.resolveService(service, mResolveListener);
}
}
#Override
public void onServiceLost(NsdServiceInfo service) {
Log.e(TAG, "service lost" + service);
if (mService == service) {
mService = null;
}
}
#Override
public void onDiscoveryStopped(String serviceType) {
Log.i(TAG, "Discovery stopped: " + serviceType);
}
#Override
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, "Discovery failed: Error code:" + errorCode);
mNsdManager.stopServiceDiscovery(this);
}
#Override
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, "Discovery failed: Error code:" + errorCode);
mNsdManager.stopServiceDiscovery(this);
}
};
}
public void initializeResolveListener() {
mResolveListener = new NsdManager.ResolveListener() {
#Override
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
Log.e(TAG, "Resolve failed" + errorCode);
}
#Override
public void onServiceResolved(NsdServiceInfo serviceInfo) {
Log.e(TAG, "Resolve Succeeded. " + serviceInfo);
if (serviceInfo.getServiceName().equals(mServiceName)) {
Log.d(TAG, "Same IP.");
return;
}
mService = serviceInfo;
}
};
}
public void discoverServices() {
mNsdManager.discoverServices(
SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
}
}
I tried calling the dicoverService in onclick button event in failed. Do we need to create a Async Task or a new Thread?
first specify the type of TCP network you want to find. Then make the object of the class and call the Intilization and then the discovery function of the NSD.
It should work just fine.
It is not necessary to run this functionality in another thread.
After some investigation current functionality stop crash and start discovery, when change String SERVICE_TYPE = "http"; to public static final String SERVICE_TYPE = "_name._tcp";
You can discover tcp service and then when you will obtain IP just connect to web server via http use those IP adress.
I want to check whether a headset has a microphone or not. Currently I'm using this broadcast receiver code, but I'm not sure whether it is correct or not.
public class HeadSetMicrophoneStateReceiver extends BroadcastReceiver {
private String pluggedState = null;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("microphone", -1);
switch (state) {
case 0:
//Headset does not have a Microphone
pluggedState = "0";
break;
case 1:
//Headset have a Microphone
pluggedState = "1";
break;
default:
pluggedState = "I have no idea what the headset state is";
}
EventBus.getDefault().post(new HeadSetMicrophoneEvent(pluggedState));
}
}
}
Please help me out.
This method returns whether the microphone is available.
If it is not available then an exception will be caught.
public static boolean getMicrophoneAvailable(Context context) {
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile(new File(context.getCacheDir(), "MediaUtil#micAvailTestFile").getAbsolutePath());
boolean available = true;
try {
recorder.prepare();
}
catch (IOException exception) {
available = false;
}
recorder.release();
return available;
}
try Following code
ackage com.example.headsetplugtest;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
public class MyActivity extends Activity {
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
Log.d("MyActivity ", "state: " + intent.getIntExtra("state", -1));
Log.d("MyActivity ", "microphone: " + intent.getIntExtra("microphone", -1));
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
getApplicationContext().registerReceiver(mReceiver, filter);
}
#Override
protected void onStop() {
super.onStop();
getApplicationContext().unregisterReceiver(mReceiver);
}
}
I know the local service runs in the UI thread of the main process, the same as activity.
According to this principle,it shouldn't work immediately when I do something in an activity, if something time-consuming is running in service.
But the example below isn't so.
Who can tell me the truth?
Much appreciate!
package com.example.testservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private static final String TAG = "LocalService";
private MyService.LocalBinder binder = new MyService.LocalBinder();
public class LocalBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
#Override
public IBinder onBind(Intent intent) {
return binder;
}
#Override
public void onCreate() {
Log.i(TAG, "onCreate");
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand");
//I do something time-consuming in here.
for (int i = 1; i <= 10; i++) {
Log.i(TAG, "Name:" + Thread.currentThread().getName()
+ ".Service i:" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return START_STICKY;
}
#Override
public void onDestroy() {
Log.i(TAG, "onDestroy");
super.onDestroy();
}
#Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "onUnbind!");
return super.onUnbind(intent);
}
}
Below is the Activity code:
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnTimeConsuming:
for(int i = 1;i <= 10; i ++)
{
Log.i(TAG, "Name:" + Thread.currentThread().getName() + ".Activity i:" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
break;
case R.id.btnStartService:
startService(intent);
break;
default:
break;
}
}
When I click the Button "R.id.btnStartService",the I click the Button "R.id.btnTimeConsuming"
It print the below Log.
The task in Service and the task in Service is complicating. Why?
I had this error :
Cannot make a static reference to the non-static method
getApplicationContext() from the type ContextWrapper
please find the method that have the error registerInGCMService(Context context)
class:
package com.example.elarabygroup;
import com.google.android.gcm.GCMBaseIntentService;
import com.google.android.gcm.GCMRegistrar;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.provider.Settings.Secure;
import android.util.Log;
public class GCMIntenetService extends GCMBaseIntentService {
private static final String GCM_SENDER_ID = "1111111111";
public GCMIntenetService() {
super();
}
#Override
protected void onRegistered(Context context, String registrationId) {
Log.i(TAG, "Device registered: regId = " + registrationId);
GCMRegistrar.setRegisteredOnServer(context, true);
}
#Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "Device unregistered");
if (GCMRegistrar.isRegisteredOnServer(context)) {
String regId = "";
Log.i(TAG, "unregistering device (regId = " + regId + ")");
GCMRegistrar.setRegisteredOnServer(context, false);
} else {
// This callback results from the call to unregister made on
// ServerUtilities when the registration to the server failed.
Log.i(TAG, "Ignoring unregister callback");
}
}
#Override
protected void onError(Context context, String errorId) {
// push error processing
}
#Override
protected void onMessage(Context arg0, Intent arg1) {
Log.i(TAG, "Received message");
Log.i(TAG, "EXTRAS" + arg1.getExtras());
// String message = getString(R.string.gcm_message);
generateNotification(arg0,
arg1.getStringExtra("Please download our new updates"));
// notifies user about message
}
private void generateNotification(Context arg0, String stringExtra) {
// TODO Auto-generated method stub
}
public static void registerInGCMService(Context context) {
GCM_SENDER_ID = Secure.getString(context.getApplicationContext().getContentResolver(),
Secure.ANDROID_ID);
if (!checkIsGCMServiceAvailable(context)) {
return;
}
final String regId = GCMRegistrar.getRegistrationId(context);
if (regId.equals("")) {
try {
GCMRegistrar.register(context, GCM_SENDER_ID);
} catch (Exception ex) {
}
} else {
// Already registered
}
}
public static boolean checkIsGCMServiceAvailable(Context context) {
try {
GCMRegistrar.checkDevice(context);
GCMRegistrar.checkManifest(context);
return true;
} catch (Throwable th) {
return false;
}
}
}
What you probably meant is :
context.getApplicationContext()
instead of
getApplicationContext()
Or you can try like this -
GCM_SENDER_ID = Secure.getString(context.getContentResolver(),
Secure.ANDROID_ID);
Sometime this error comes, when we are using "getConTentResolver()" in static method
like:
public static void Mthd()
{
Cursor cursor =getContentResolver().query(uri, null, null, null, null);
//ur next code
}
So, in this case it will give an error, Therefore we have to make the function non-static.