简体   繁体   English

Android工作室项目到Unity 5

[英]Android studio project to Unity 5

I made a notification service project in android studio which runs perfect on lollipop. 我在android studio中做了一个通知服务项目,在棒棒糖上运行完美。 now I'm trying to make it into a plugin for Unity 5. but once build it keeps crashing on start up... 现在我正在尝试将它变成Unity 5的插件。但是一旦构建它就会在启动时不断崩溃......

AndroidManifest.xml: 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 class: 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");

}
}

MainActivity: 主要活动:

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: 的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)

I can't seem to find which one of these is responsible for the crash. 我似乎无法找到其中哪一个导致崩溃。 :-( :-(

found out what's wrong, so i'll post it as answer. 发现了什么是错的,所以我会把它作为答案发布。 making an Android Studio application into a Unity 5 plugin: 将Android Studio应用程序制作成Unity 5插件:

so first of all you need to turn the project into a library. 首先,您需要将项目转换为库。 you can do this by copying the Unity classes.jar into the app/libs folder. 您可以通过将Unity classes.jar复制到app / libs文件夹中来完成此操作。 you can find the classes.jar on pc in 你可以在pc上找到classes.jar

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

and on Mac 在Mac上

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

rightclick on the classes.jar in your Android Studio app and select Add as Library. 右键单击Android Studio应用程序中的classes.jar,然后选择Add as Library。 Add it to your app. 将其添加到您的应用中。

open the build.gradle from your app folder and paste this add the bottom of it. 从你的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)

then sync it. 然后同步它。 now if you open the gradle tab on the right and navigate the "other" task folder to find the exportJar task. 现在,如果您打开右侧的gradle选项卡并导航“other”任务文件夹以查找exportJar任务。 double clicking it will build you a AndroidPlugin.jar in app/release this together with your Manifest.xml should go into unity, in Assets/Plugins/Android (you create the folders you need) 双击它会在app / release中创建一个AndroidPlugin.jar这个与你的Manifest.xml一起进入统一,在Assets / Plugins / Android中(你创建你需要的文件夹)

for a recap i got it from this 为回顾我是从这个

now for the part that confused me in converting the project. 现在对于让我在转换项目时感到困惑的部分。 you always need a class in your Android Studio project that extends UnityPlayerActivity. 您总是需要Android Studio项目中的一个扩展UnityPlayerActivity的类。 This is the bridge between your unity project and your android studio project. 这是你的团结项目和你的android工作室项目之间的桥梁。

so that you can compare the changes with the above code, i'll repost the code that worked (the manifest and the gradle almost stayed the same the big change was in the MainActivity.java: 为了能够将更改与上面的代码进行比较,我将重新发布有效的代码(清单和gradle几乎保持相同,MainActivity.java中的重大变化:

so MainActivity class: 所以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;
    }
}

the NotificationService class: 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");

    }
}

this is my PluginController script in unity: 这是我的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