簡體   English   中英

Android工作室項目到Unity 5

[英]Android studio project to Unity 5

我在android studio中做了一個通知服務項目,在棒棒糖上運行完美。 現在我正在嘗試將它變成Unity 5的插件。但是一旦構建它就會在啟動時不斷崩潰......

AndroidManifest.xml中:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.techlab.samanthaplugin" >

<application
    android:allowBackup="true"
    android:label="SamanthaPlugin">
    <activity
        android:name="com.techlab.samanthaplugin.MainActivity"
        android:label="SamanthaPlugin" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name="com.techlab.samanthaplugin.NotificationService"
             android:label="SamanthaPlugin"
             android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">

    <intent-filter>

        <action android:name="android.service.notification.NotificationListenerService" />

    </intent-filter>

</service>

</application>

NotificationService類:

package com.techlab.samanthaplugin;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
import android.support.v4.content.LocalBroadcastManager;


public class NotificationService extends NotificationListenerService {

Context context;

@Override

public void onCreate() {

    super.onCreate();
    context = getApplicationContext();

}
@Override

public void onNotificationPosted(StatusBarNotification sbn) {


    String pack = sbn.getPackageName();
    String ticker = sbn.getNotification().tickerText.toString();
    Bundle extras = sbn.getNotification().extras;
    String title = extras.getString("android.title");
    String text = extras.getCharSequence("android.text").toString();

    Log.i("Package",pack);
    Log.i("Ticker",ticker);
    Log.i("Title",title);
    Log.i("Text",text);

    Intent msgrcv = new Intent("Msg");
    msgrcv.putExtra("package", pack);
    msgrcv.putExtra("ticker", ticker);
    msgrcv.putExtra("title", title);
    msgrcv.putExtra("text", text);

    LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);


}

@Override

public void onNotificationRemoved(StatusBarNotification sbn) {
    Log.i("Msg","Notification Removed");

}
}

主要活動:

package com.techlab.samanthaplugin;

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.support.v4.content.LocalBroadcastManager;


public class MainActivity extends Activity {

//TableLayout tab;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    //tab = (TableLayout)findViewById(R.id.tab);
    LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, new IntentFilter("Msg"));

}


private BroadcastReceiver onNotice= new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        String pack = intent.getStringExtra("package");
        String title = intent.getStringExtra("title");
        String text = intent.getStringExtra("text");



            //TableRow tr = new TableRow(getApplicationContext());
            //tr.setLayoutParams(new TableRow.LayoutParams( TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
            //TextView textview = new TextView(getApplicationContext());
            //textview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT,1.0f));
            //textview.setTextSize(20);
            //textview.setTextColor(Color.parseColor("#0B0719"));
            //textview.setText(Html.fromHtml(pack +"<br><b>" + title + " : </b>" + text));
            //tr.addView(textview);
            //tab.addView(tr);
    }
};
}

的build.gradle:

apply plugin: 'com.android.library'

android {
compileSdkVersion 21
buildToolsVersion "21.0.2"

defaultConfig {
    //applicationId "techlab.samanthaplugin"
    minSdkVersion 19
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services:6.1.71'
compile files('libs/classes.jar')
}
//task to delete the old jar
task deleteOldJar(type: Delete) {
delete 'release/AndroidPlugin.jar'
}

//task to export contents as jar
task exportJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('release/')
include('classes.jar')
///Rename the jar
rename('classes.jar', 'AndroidPlugin.jar')
}

exportJar.dependsOn(deleteOldJar, build)

我似乎無法找到其中哪一個導致崩潰。 :-(

發現了什么是錯的,所以我會把它作為答案發布。 將Android Studio應用程序制作成Unity 5插件:

首先,您需要將項目轉換為庫。 您可以通過將Unity classes.jar復制到app / libs文件夾中來完成此操作。 你可以在pc上找到classes.jar

C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androidplayer\release\bin

在Mac上

Applications\Unity(rightclick Show Package Content)PlaybackEngines\AndroidPlayer\Variations\Release\Classes

右鍵單擊Android Studio應用程序中的classes.jar,然后選擇Add as Library。 將其添加到您的應用中。

從你的app文件夾中打開build.gradle並粘貼它添加它的底部。

//task to delete the old jar
task deleteOldJar(type: Delete) {
    delete 'release/AndroidPlugin.jar'
}

//task to export contents as jar
task exportJar(type: Copy) {
    from('build/intermediates/bundles/release/')
    into('release/')
    include('classes.jar')
    ///Rename the jar
    rename('classes.jar', 'AndroidPlugin.jar')
}

exportJar.dependsOn(deleteOldJar, build)

然后同步它。 現在,如果您打開右側的gradle選項卡並導航“other”任務文件夾以查找exportJar任務。 雙擊它會在app / release中創建一個AndroidPlugin.jar這個與你的Manifest.xml一起進入統一,在Assets / Plugins / Android中(你創建你需要的文件夾)

為回顧我是從這個

現在對於讓我在轉換項目時感到困惑的部分。 您總是需要Android Studio項目中的一個擴展UnityPlayerActivity的類。 這是你的團結項目和你的android工作室項目之間的橋梁。

為了能夠將更改與上面的代碼進行比較,我將重新發布有效的代碼(清單和gradle幾乎保持相同,MainActivity.java中的重大變化:

所以MainActivity類:

package com.techlab.pluginsamantha;

import com.unity3d.player.UnityPlayerActivity;

public class MainActivity extends UnityPlayerActivity {

    public String GetSenderName()
    {
        String senderName = NotificationService.sender;
        return senderName;
    }
    public String GetAppName()
    {
        String appName = NotificationService.appName;
        return appName;
    }
    public String GetTitle()
    {
        String textTitle = NotificationService.titleText;
        return textTitle;
    }
    public String GetBody()
    {
        String textBody = NotificationService.bodyText;
        return textBody;
    }
    public boolean GetNewNotification()
    {
        return NotificationService.newNotification;
    }
}

NotificationService類:

package com.techlab.pluginsamantha;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
import android.support.v4.content.LocalBroadcastManager;


public class NotificationService extends NotificationListenerService {

    Context context;
    public static String appName;
    public static String sender;
    public static String titleText;
    public static String bodyText;
    public static boolean newNotification;

    @Override

    public void onCreate() {

        super.onCreate();
        context = getApplicationContext();

    }
    @Override

    public void onNotificationPosted(StatusBarNotification sbn) {

        newNotification = true;
        String pack = sbn.getPackageName();
        appName = pack;
        String ticker = sbn.getNotification().tickerText.toString();
        sender = ticker;
        Bundle extras = sbn.getNotification().extras;
        String title = extras.getString("android.title");
        titleText = title;
        String text = extras.getCharSequence("android.text").toString();
        bodyText = text;

        newNotification = false;

    }

    @Override

    public void onNotificationRemoved(StatusBarNotification sbn) {
        Log.i("Msg","Notification Removed");

    }
}

這是我的PluginController腳本統一:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Runtime.InteropServices;

public class PluginController : MonoBehaviour {

    public GUISkin skin;
    public List<Notification> notifications = new List<Notification>();
    string bodyText = "";

    public string GetSenderName()
    {
        AndroidJavaClass unity = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject> ("currentActivity");
        return currentActivity.Call<string>("GetSenderName");
    }
    public string GetAppName()
    {
        AndroidJavaClass unity = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject> ("currentActivity");
        return currentActivity.Call<string>("GetAppName");
    }

    public string GetTitleText()
    {
        AndroidJavaClass unity = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject> ("currentActivity");
        return currentActivity.Call<string>("GetTitle");
    }
    public string GetBodyText()
    {
        AndroidJavaClass unity = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject> ("currentActivity");
        return currentActivity.Call<string>("GetBody");
    }
    public bool GetNewNotification()
    {
        AndroidJavaClass unity = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject> ("currentActivity");
        return currentActivity.Call<bool>("GetNewNotification");
    }
    void Update()
    {
        if(GetNewNotification())
            CopyNotification();
    }
    public void CopyNotification()
    {
        if(bodyText != GetBodyText())
        {
            Notification note = new Notification();
            note.SenderName = GetSenderName();
            note.AppName = GetAppName();
            note.TitleText = GetTitleText();
            note.TitleLength = note.TitleText.Length;
            note.BodyText = GetBodyText();
            note.BodyLength = note.BodyText.Length;
            notifications.Add(note);
            bodyText = GetBodyText();
        }
    }
    void OnGUI()
    {
        GUI.skin = skin;
        foreach(Notification n in notifications)
        {
            GUILayout.Label("SenderName: "+n.SenderName);
            GUILayout.Label("AppName: "+n.AppName);
            GUILayout.Label("TitleText: "+n.TitleText+" :length: "+n.TitleLength.ToString());
            GUILayout.Label("BodyText: "+n.BodyText+" :length: "+n.BodyLength.ToString());
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM