Wednesday, March 5, 2014

Android first post, the truth for help.


I would like to start the login screen when the monitor network status changes, my code is as follows:

 
public class LoginActivity extends Activity {
InternetListener.IntenetBinder binder;
private ServiceConnection conn = new ServiceConnection () {

@ Override
public void onServiceDisconnected (ComponentName name) {
binder = null;
}

@ Override
public void onServiceConnected (ComponentName name, IBinder service) {
binder = (IntenetBinder) service;
}
};

@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_load);
final Intent intent = new Intent (this, InternetListener.class);
bindService (intent, conn, Service.BIND_AUTO_CREATE); / / bind service
NetworkInfo info = binder.getNetStatus ();
if (info! = null && info.isAvailable ()) {
new BaiDuOpenAPI () login (this);.
}
}

}


This is the activity class
 
public class InternetListener extends Service {
private ConnectivityManager connectivityManager;
private NetworkInfo info;
private IntenetBinder binder = new IntenetBinder ();
private BroadcastReceiver receiver = new BroadcastReceiver () {

@ Override
public void onReceive (Context context, Intent intent) {
String action = intent.getAction ();
if (action.equals (ConnectivityManager.CONNECTIVITY_ACTION)) {
connectivityManager = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);
info = connectivityManager.getActiveNetworkInfo ();
if (info! = null && info.isAvailable ()) {
String name = info.getTypeName ();
Log.d ("mark", "current network name:" + name);
} Else {
Log.d ("mark", "Network not available");
}
}
}

};

public class IntenetBinder extends Binder {
/ **
* Return the network status
*
* @ Return
* /
public NetworkInfo getNetStatus () {
return info;
}
}

@ Override
public IBinder onBind (Intent intent) {
return binder;
}

@ Override
public void onCreate () {
super.onCreate ();
IntentFilter mFilter = new IntentFilter ();
mFilter.addAction (ConnectivityManager.CONNECTIVITY_ACTION);
/ / RegisterReceiver (receiver, mFilter);
}

@ Override
public void onDestroy () {
unregisterReceiver (receiver);
}

@ Override
public int onStartCommand (Intent intent, int flags, int startId) {
return super.onStartCommand (intent, flags, startId);

}
}

This is a service class

But in actual service bindings attempt did not respond, can not always go in service in onCreate () method, I also checked the Android official document or puzzled.

Ask colleagues to help us to see the problem in that.
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
service is registered
Reply:
Existing broadcast network changes have you write a class monitor trouble themselves to death
Dynamic landing when using IntentFilter Register Login finished look you need to cancel the registration at the appropriate time
Reply:
No recent onCreate () service failed to start only should you not registered it
Reply:
reference to the third floor lovexjyong reply:
no recent onCreate () only service failed to start your registration should be no bar




This is my registration service, so there is no problem to write?
Reply:
reference to the second floor tanwei4199 reply:
broadcast network changes have you ready to write your own classes to monitor trouble dead
Dynamic landing when using IntentFilter Register Login finished look you need to cancel the registration

at the appropriate time
Understand what you mean, but I just want to practice using the service component Ha, thank you for your reply.

I feel my debugging for a long time to write without errors do not always know why bind unsuccessful.
Reply:
references, 5th Floor Yweige2010 reply:
Quote: references to the second floor tanwei4199 reply:

Existing broadcast network changes have you write a class monitor trouble themselves to death
Dynamic landing when using IntentFilter Register Login finished look you need to cancel the registration

at the appropriate time
Understand what you mean, but I just want to practice using the service component Ha, thank you for your reply.

I feel my debugging for a long time to write without errors do not always know why bind unsuccessful.

Your Service a bit strange ah you use the Service Binder that you should inherit is aidl compiler generated service interfaces ah
This direct use startService stopService you can ah
Reply:

No permission to add the network?
Reply:
Wrong wrong ...... you. . . .
@ Override
public IBinder onBind (Intent intent) {
return new IntenetBinder ();
}
public class IntenetBinder extends Binder {
/ **
* Return the network status
*
* @ Return
* /
public InternetListener getService () {
return new IntenetBinder ();
}
}



private ServiceConnection conn = new ServiceConnection () {

@ Override
public void onServiceDisconnected (ComponentName name) {
binder = null;
}

@ Override
public void onServiceConnected (ComponentName name, IBinder service) {
(IntenetBinder) service.getService ();
}
};

getNetStatus () method should be the service, rather than a method binder;
Reply:
service is registered, is bound to repeat
Reply:
reference to the 6th floor tanwei4199 reply:
Quote: references, 5th Floor Yweige2010 reply:

Quote: references to the second floor tanwei4199 reply:

Existing broadcast network changes have you write a class monitor trouble themselves to death
Dynamic landing when using IntentFilter Register Login finished look you need to cancel the registration

at the appropriate time
Understand what you mean, but I just want to practice using the service component Ha, thank you for your reply.

I feel my debugging for a long time to write without errors do not always know why bind unsuccessful.

Your Service a bit strange ah you use the Service Binder that you should inherit is aidl compiler generated service interfaces ah
This direct use startService stopService you can ah


It is not cross-process calls aidl I call a local service, so that activity and service communication, startService do it?
Reply:
reference to the 8th floor wch18956441322 reply:
Wrong wrong ...... you. . . .
@ Override
public IBinder onBind (Intent intent) {
return new IntenetBinder ();
}
public class IntenetBinder extends Binder {
/ **
* Return the network status
*
* @ Return
* /
public InternetListener getService () {
return new IntenetBinder ();
}
}



private ServiceConnection conn = new ServiceConnection () {

@ Override
public void onServiceDisconnected (ComponentName name) {
binder = null;
}

@ Override
public void onServiceConnected (ComponentName name, IBinder service) {
(IntenetBinder) service.getService ();
}
};

getNetStatus () method should be a service, rather than the binder method;


Brothers getNetStatus This method is what I wrote ah I inherited binder is then added to this method, how to become a service approach?
Reply:
reference to the 9th floor jianfeng4113 reply:
service is registered, if repeated bind


service registered no duplicate bindings binding code stickers out.



reference to the 7th floor qiuqingpo reply:

No permission to add the network?


The authority added Ha
Reply:
There is no error logcat you look about her. . . .
Reply:
reference to the 13th floor yuleyouxi reply:
you look logcat have given her. . . .


Not wrong brother

Which Great God help me find ah?
Reply:
reference to the 10th floor Yweige2010 reply:
Quote: references to the 6th floor tanwei4199 reply:

Quote: references, 5th Floor Yweige2010 reply:

Quote: references to the second floor tanwei4199 reply:

Existing broadcast network changes have you write a class monitor trouble themselves to death
Dynamic landing when using IntentFilter Register Login finished look you need to cancel the registration

at the appropriate time
Understand what you mean, but I just want to practice using the service component Ha, thank you for your reply.

I feel my debugging for a long time to write without errors do not always know why bind unsuccessful.

Your Service a bit strange ah you use the Service Binder that you should inherit is aidl compiler generated service interfaces ah
This direct use startService stopService you can ah


It is not cross-process calls aidl I call a local service, so that activity and service communication, startService do it?

You can ... startService
Reply:
reference to the 14th floor Yweige2010 reply:
Quote: references to the 13th floor yuleyouxi reply:

There is no error logcat you look about her. . . .


Not wrong brother

Which Great God help me find ah?


If the service does not usually have given up right
Or you try to add a little log or directly with a single step debugging went to see where
Reply:
reference to the 16th floor yuleyouxi reply:
Quote: references to the 14th floor Yweige2010 reply:

Quote: references to the 13th floor yuleyouxi reply:

There is no error logcat you look about her. . . .


Not wrong brother

Which Great God help me find ah?


If the service does not usually have given up right
Or you try to add a little log or directly go to step through to see the


Really there is no error, which started service through the normal step, but that is not starting up

No comments:

Post a Comment