如何将 Java 语法转换为 C# of Myclass obj=new Myclass() public override mymethod()
Posted
技术标签:
【中文标题】如何将 Java 语法转换为 C# of Myclass obj=new Myclass() public override mymethod()【英文标题】:How to convert Java Syntax to C# of Myclass obj=new Myclass() public override mymethod() 如何将 Java 语法转换为 C# of Myclass obj=new Myclass() public override mymethod() 【发布时间】:2020-04-04 18:03:21 【问题描述】:我想将 Java 代码转换为 C#,但这样做会遇到问题
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();
这部分在代码中间我没有得到
BroadcastReceiver receiver = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
;
【问题讨论】:
【参考方案1】:您可能希望看到一个更全面的答案,以了解给定的建议如何组合在一起:
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;
【讨论】:
【参考方案2】:我认为你会做这样的事情
var receiver = new MyReceiver();
public class MyReceiver : BroadcastReceiver
public override void onReceive(Context context, Intent intent)
【讨论】:
【参考方案3】:由于 C# 不允许匿名类,因此您必须显式创建它,然后从该类传递您需要的数据。
[BroadcastReceiver(Enabled = true, Exported = false)]
public class SampleReceiver : BroadcastReceiver
public override void OnReceive(Context context, Intent intent)
// Do stuff here.
然后像这样使用它:
var receiver = new SampleReceiver();
【讨论】:
外面会是一个新类,实际检查docs.microsoft.com/en-us/xamarin/android/app-fundamentals/…以上是关于如何将 Java 语法转换为 C# of Myclass obj=new Myclass() public override mymethod()的主要内容,如果未能解决你的问题,请参考以下文章