unity 3d 开启服务 实现本地推送

任务:unity 工具实现 游戏本地推送 ,不要网络状态

 

如何做:

 

Demo:

 

 

一,currentActivity unity 调用的Activity

@Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ctx = UnityPlayer.currentActivity;
  cont = this;
  Log.e(TAG, "currentActivity onCreate --------------------");

//前台时。。。屏幕常亮3句话。。。mf文件也需要配置
  PowerManager manager = ((PowerManager)getSystemService(POWER_SERVICE));
  WakeLock wakeLock = manager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK| PowerManager.ON_AFTER_RELEASE, "ATAAW");
  wakeLock.acquire();


 }

 

 

 

public void InputParams(int clock, int type, String info) {
  MyService.f=1;          // 服务状态开启标记
  Log.e(TAG, "currentActivity InputParams --------------------");
  Intent intent = new Intent();
  intent.setAction("com.lzg.tw.BIND_SERVICE");
  Bundle bundle = new Bundle();
  bundle.putInt("clock", clock);// 发送数据
  bundle.putInt("type", type);// 发送数据
  bundle.putString("info", info);// 发送数据
  Log.e(TAG, "---" + clock + "---" + type + "---" + info);
  intent.putExtras(bundle);
  startService(intent);
  Log.e(TAG, "currentActivity InputParams ---");
 }

 

@Override
 protected void onStop() {
  super.onStop();
  Log.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity 服务器通信协议可以使用多种方式,例如: 1. TCP/IP协议:通过使用Socket或TcpClient等Unity自带的类库实现。 2. UDP协议:通过使用UdpClient等Unity自带的类库实现。 3. HTTP协议:通过使用HttpWebRequest或HttpClient等Unity自带的类库实现。 而在Unity实现HTTP通信,可以使用以下步骤: 1. 在Unity中创建一个空对象,并为其添加一个C#脚本。 2. 在脚本中使用HttpWebRequest或HttpClient等Unity自带的类库,发送HTTP请求。 3. 处理服务器返回的响应,例如解析JSON或XML数据。 以下是一个简单的HTTP GET请求的示例代码: ```csharp using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Net; using System.IO; public class HttpExample : MonoBehaviour { // URL to request private const string url = "https://api.github.com/"; // Start is called before the first frame update void Start() { // Create a new HttpWebRequest HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; // Send the request and get the response HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Read the response stream Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseText = reader.ReadToEnd(); // Log the response to the console Debug.Log(responseText); // Clean up resources reader.Close(); dataStream.Close(); response.Close(); } } ``` 该示例使用HttpWebRequest类发送了一个HTTP GET请求,并打印了响应文本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值