如何将Java语法转换为C#的Myclass obj=new Myclass(){ public override mymethod() } [英] How to convert Java Syntax to C# of Myclass obj=new Myclass(){ public override mymethod() }

查看:23
本文介绍了如何将Java语法转换为C#的Myclass obj=new Myclass(){ public override mymethod() }的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 Java 代码转换为 C# 但遇到了问题

I want to convert Java code to C# but facing problem to do so

public class MyService extends Service {

  static final String CONNECTIVITY_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
  NotificationManager manager ;

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // Let it continue running until it is stopped.
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (CONNECTIVITY_CHANGE_ACTION.equals(action)) {
                //check internet connection
                if (!ConnectionHelper.isConnectedOrConnecting(context)) {
                    if (context != null) {
                        boolean show = false;
                        if (ConnectionHelper.lastNoConnectionTs == -1) {//first time
                            show = true;
                            ConnectionHelper.lastNoConnectionTs = System.currentTimeMillis();
                        } else {
                            if (System.currentTimeMillis() - ConnectionHelper.lastNoConnectionTs > 1000) {
                                show = true;
                                ConnectionHelper.lastNoConnectionTs = System.currentTimeMillis();
                            }
                        }

                        if (show && ConnectionHelper.isOnline) {
                            ConnectionHelper.isOnline = false;
                            Log.i("NETWORK123","Connection lost");
                            //manager.cancelAll();
                        }
                    }
                } else {
                    Log.i("NETWORK123","Connected");
                    showNotifications("APP" , "It is working");
                    // Perform your actions here
                    ConnectionHelper.isOnline = true;
                }
            }
        }
    };
    registerReceiver(receiver,filter);
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
}

这部分我没有得到代码中间的内容

This part I am not getting which coming in middle of code

BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent)
    {

    }
};

推荐答案

您可能希望看到更全面的答案,以了解给定的建议如何组合在一起:

You probably want to see a more comprehensive answer for this to see how the given suggestions all fits together:

public class MyService : Service
{
  internal const string CONNECTIVITY_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
  internal NotificationManager manager;

  public override IBinder onBind(Intent intent)
  {
    return null;
  }

  public override int onStartCommand(Intent intent, int flags, int startId)
  {
    // Let it continue running until it is stopped.
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
    BroadcastReceiver receiver = new BroadcastReceiverAnonymousInnerClass();
    registerReceiver(receiver,filter);
    return START_STICKY;
  }

  private class BroadcastReceiverAnonymousInnerClass : BroadcastReceiver
  {
      public override void onReceive(Context context, Intent intent)
      {
          string action = intent.Action;
          if (CONNECTIVITY_CHANGE_ACTION.Equals(action))
          {
              //check internet connection
              if (!ConnectionHelper.isConnectedOrConnecting(context))
              {
                  if (context != null)
                  {
                      bool show = false;
                      if (ConnectionHelper.lastNoConnectionTs == -1)
                      { //first time
                          show = true;
                          ConnectionHelper.lastNoConnectionTs = DateTimeHelper.CurrentUnixTimeMillis();
                      }
                      else
                      {
                          if (DateTimeHelper.CurrentUnixTimeMillis() - ConnectionHelper.lastNoConnectionTs > 1000)
                          {
                              show = true;
                              ConnectionHelper.lastNoConnectionTs = DateTimeHelper.CurrentUnixTimeMillis();
                          }
                      }

                      if (show && ConnectionHelper.isOnline)
                      {
                          ConnectionHelper.isOnline = false;
                          Log.i("NETWORK123","Connection lost");
                          //manager.cancelAll();
                      }
                  }
              }
              else
              {
                  Log.i("NETWORK123","Connected");
                  showNotifications("APP", "It is working");
                  // Perform your actions here
                  ConnectionHelper.isOnline = true;
              }
          }
      }
  }

  public override void onDestroy()
  {
    base.onDestroy();
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
  }
}

internal static class DateTimeHelper
{
    private static readonly System.DateTime Jan1st1970 = new System.DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc);
    public static long CurrentUnixTimeMillis()
    {
        return (long)(System.DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
    }
}

这篇关于如何将Java语法转换为C#的Myclass obj=new Myclass(){ public override mymethod() }的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆