How to set a different java file as launcher - android

I am new to android and i have created a class in my android project other than the default MainActivity class and i want to start my project with that file. Here is what i added to my manifest file:
<activity
android:name="com.example.surfaceview.SurfaceViewExample"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SURFACEVIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And the class that i created:
public class SurfaceViewExample extends Activity implements OnTouchListener {
OurView v;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
v=new OurView(this);
setContentView(v);
v.setOnTouchListener(this);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
v.pause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
v.resume();
}
public class OurView extends SurfaceView implements Runnable{
Thread t = null;
SurfaceHolder holder;
boolean isItOK=false;
public OurView(Context context) {
super(context);
holder=getHolder();
}
#Override
public void run() {
while(isItOK){
if(holder.getSurface().isValid()){
continue;
}
Canvas c = holder.lockCanvas();
c.drawARGB(255, 155, 155, 10);//canvas backgroundu boyama
holder.unlockCanvasAndPost(c);
}
}
public void pause(){ //pause the thread
isItOK=false;
while(true){
try{
t.join();
}catch(InterruptedException e){
e.printStackTrace();
}
break;
}
t=null;
}
public void resume(){ //resume the thread
isItOK=true;
t=new Thread(this); //this parameter means use this run method
//which is inside that class
t.start();
}
}
#Override
public boolean onTouch(View v, MotionEvent me) {
return false;
}
}
But the application does not start. I think the problem might be in that line inside the intent filter:
<action android:name="android.intent.action.SURFACEVIEW" />
Can anyone help me with this?
Thanks

Update your manifest with the following code for your activity. This should now launch your activity.
<activity
android:name="com.example.surfaceview.SurfaceViewExample"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I hope this helps.

Related

UnityPlayer blackScreen on a subActivity

On a android Studio project, when i switch to a another Activity who contains also a UnityPlayer, the UnityPlayer display a BlackScreen on the subActivity.
I am on Arcore project where i use Android studio for have a good interface and easy to implement, i use the last Unity version and Android studio, and every last android librairy(Androidx).
I needed to open the Unity view on a another activity, but do not seems to working. And every research done on google send me to answer who do not resolve my problem or else to add a android:process on my manifest.xml, but impossible to get it to work.
Every code related to mUnityPlayer
Manifest.xml of the two activity:
[...]
<activity
android:name=".MainActivity"
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density"
android:hardwareAccelerated="false"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:theme="#style/AppTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
<meta-data
android:name="unityplayer.UnityActivity"
android:value="true" />
</activity>
<activity
android:name=".ViewerPlaneActivity"
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density"
android:hardwareAccelerated="false"
android:label=""
android:screenOrientation="landscape"
android:theme="#style/AppTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
<meta-data
android:name="unityplayer.UnityActivity"
android:value="true" />
</activity>
[...]
MainActivity of my application:
public class MainActivity extends AppCompatActivity {
[...]
public UnityPlayer mUnityPlayer;
public static UnityPlayerActivity currentActivity;
private boolean switchToAnAnotherUnityAct=false;
[...]
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mUnityPlayer = new UnityPlayer(this);
setContentView(R.layout.viewer_view_simple_viewer);
replaceUnityPlayerOnFrameLayout();
mUnityPlayer.getView().requestFocus();
currentActivity = this;
[...]
}
private void replaceUnityPlayerOnFrameLayout(){
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
if( mUnityPlayer.getView().getParent()==null)
frameLayout.addView(mUnityPlayer.getView());
}
#Override
protected void onDestroy() {
if(!switchToAnAnotherUnityAct)mUnityPlayer.destroy();
super.onDestroy();
}
#Override
protected void onPause() {
super.onPause();
if(!switchToAnAnotherUnityAct)mUnityPlayer.pause();
}
#Override
protected void onResume() {
super.onResume();
if(!switchToAnAnotherUnityAct){
mUnityPlayer.resume();
replaceUnityPlayerOnFrameLayout();
}
}
#Override
protected void onStart() {
super.onStart();
mUnityPlayer.start();
switchToAnAnotherUnityAct=false;
}
#Override
protected void onStop() {
super.onStop();
if(!switchToAnAnotherUnityAct)mUnityPlayer.stop();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mUnityPlayer.lowMemory();
}
#Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
if (level == TRIM_MEMORY_RUNNING_CRITICAL) {
mUnityPlayer.lowMemory();
}
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
public void onClickSizeSettings(View view) {
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
frameLayout.removeAllViews();
switchToAnAnotherUnityAct=true;
Intent intent = new Intent(this, ViewerPlaneActivity.class);
this.startActivity(intent);
}
[...]
The sub-activity:
public class ViewerPlaneActivity extends AppCompatActivity {
public UnityPlayer mUnityPlayer;
public static ViewerPlaneActivity currentActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentActivity = this;
mUnityPlayer= MainActivity.currentActivity.mUnityPlayer;
setContentView(R.layout.viewer_plane_unit);
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
frameLayout.addView(mUnityPlayer.getView());
mUnityPlayer.getView().requestFocus();
[...]
}
private void replaceUnityPlayerOnFrameLayout(){
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
if( mUnityPlayer.getView().getParent()==null)
frameLayout.addView(mUnityPlayer.getView());
}
#Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
if (level == TRIM_MEMORY_RUNNING_CRITICAL) {
mUnityPlayer.lowMemory();
}
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
return mUnityPlayer.injectEvent(event);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return mUnityPlayer.injectEvent(event);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
return mUnityPlayer.injectEvent(event);
}
public void onClickAnchorPlane(View view){
[...]
finish();
}
#Override
public void finish() {
super.finish();
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
frameLayout.removeAllViews();
}
#Override
protected void onPause() {
super.onPause();
mUnityPlayer.pause();
}
#Override
protected void onResume() {
super.onResume();
replaceUnityPlayerOnFrameLayout();
mUnityPlayer.resume();
}
#Override
protected void onStart() {
super.onStart();
mUnityPlayer.start();
}
The result than i expect is to see the UnityPlayer displayed, but i got just a blackscreen. And zero errors.
I precise than i do not get problem for return on my MainActivity where the UnityPlayer work and display correctly the viewer. Just the SecondViewer, where i got a blackscreen where i expect just to see the unity to display my plane and the camera.
Thank for your help.
For finish, i have found no solution to my problem so i have used two ViewStub on my main layout.

activity main MAIN and LAUNCHER is not fired first

I have a launcher activity followed by loginActivity. But everytime I start my APP my loginActivity is fired up first rather than my launcherActivity.
The xml for manifest.xml is like
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kryptapps.konel.dchat2" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/CustomTheme" >
<activity
android:name=".LauncherActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login" >
</activity>
<activity
android:name=".GroupListActivity"
android:label="#string/title_activity_group_list" >
</activity>
</application>
</manifest>
java file is like :
public class LauncherActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
loadPersonalDatas();
Thread time = new Thread() {
public void run() {
try {
sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
time.start();
}
void loadPersonalDatas(){
new Thread() {
public void run() {
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
the login activity is like usual log in page.
Actually LauncherActivity is getting fired, but just for 500 milliseconds.
Whats happening is the wait for 20000 milliseconds and the execution of loadPersonalDatas() is carried out simultaneously.
But after 500 milliseconds, the activity changes and sleep(20000) is interrupted.
For the LoginActivity to launch after 20000 milliseconds, you should start the LoginActivity after the sleep(20000) in the run() method.
public class LauncherActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
Thread time = new Thread() {
public void run() {
try {
sleep(20000);
loadPersonalDatas(); //start activity here
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
time.start();
}
void loadPersonalDatas(){
new Thread() {
public void run() {
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
Now, the LoginActivity will start after 20500 milliseconds.

Application not loading after Splash screen

I am having an issue where the application does not load after including the Splash screen to the application. This is the code for the SplashScreen.java & the Manifest file. I am not sure what I am missing. I have gone over this several times and am unable to spot the error. I have gone thro' similar posts but could find the answer. Please help
public class SplashScreen extends Activity {
private long ms=0;
private long splashTime=2000;
private boolean splashActive = true;
private boolean paused=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread mythread = new Thread() {
public void run() {
try {
while (splashActive && ms < splashTime) {
if(!paused)
ms=ms+100;
sleep(100);
}
} catch(Exception e) {}
finally {
Intent intent = new Intent(SplashScreen.this, TotalControl.class);
startActivity(intent);
}
}
};
mythread.start();
}
The Manifest file is as follows
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.tvganesh.totalcontrol.SplashScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.tvganesh.totalcontrol.TotalControl"
android:label="#string/app_name" >
</activity>
</application>
After the Splash Screen which it display it is supposed to switch to TotalControl.class. But the I get the message "The Application stopped unexpectedly"
Can you let me know what I am missing?
Don't use Thread.sleep - you have no guarantee that it will start up again exactly after 100ms. Use a Handler instead:
getWindow().getDecorView().getHandler().postDelayed(new Runnable()
{
#Override
public void run()
{
Intent intent = new Intent(SplashScreen.this, TotalControl.class);
startActivity(intent);
}
}, splashTime);
I think this will work for you
Don't use threads to show the splash.
public class SplashScreen extends Activity {
protected int _splashTime = 5000;
private Thread splashTread;
MyCount counter = new MyCount(4000, 4000);
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
counter.start();
}
public class MyCount extends CountDownTimer
{
public MyCount(long csecond, long countDownInterval)
{
super(csecond, countDownInterval);
}
#Override
public void onFinish() {
finish();
Intent intent = new Intent();
intent.setClass(SplashScreen.this, TotalControl.class);
startActivity(intent);
}
#Override
public void onTick(long arg0) {
}
}
}
I have solved the problem. This appears to be a known bug in AndEngine and the fix is add the following line to the Manifest file
android:configChanges="orientation|screenSize"
Thanks for all your help.

I'm getting ClassNotFound exception in BroadcastReceiver

I'm trying to use this very simple Service and BroadcastReceiver but I'm getting a
ClassNotFound exception. If I don't use startService things are fine so the problem it's with the receiver. I have registered it in the android manifest file so why do I get the ClassNotFound exception?
I will be using this method of communication for polling a php file and updating a ListView. Is this the appropriate way of communicating (broadcast intent)?
public class MessengerServiceActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startService(new Intent(this,MessengerService.class));
}
class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle b=intent.getExtras();
if(b==null)
return;
String msg=b.getString("message");
TextView tv=(TextView) findViewById(R.id.textView1);
tv.setText(msg);
}
}
}
public class MessengerService extends Service {
HttpClient hc;
HttpPost hp;
String s[]={"pratik","harsha","dayal","hritika"};
public MessengerService() {
// TODO Auto-generated constructor stub
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
hc=new DefaultHttpClient();
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Thread t=new Thread(new Runnable(){
public void run()
{
int k=0;
while(true)
{
Intent i=new Intent("com.pdd.messenger.MyAction");
i.putExtra("message",s[k]);
sendBroadcast(i);
try
{
Thread.sleep(3000);
}
catch (InterruptedException e)
{
Toast.makeText(getApplicationContext(), e.getLocalizedMessage(), Toast.LENGTH_LONG)
.show();
}
k=(k+1)%4;
}
}
});
t.start();
return super.onStartCommand(intent, flags, startId);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pdd.messenger"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".MessengerServiceActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyReceiver" android:enabled="true">
<intent-filter>
<action android:name="com.pdd.messenger.MyAction"/>
</intent-filter>
</receiver>
<service android:name=".MessengerService"></service>
</application>
</manifest>

Android: Search Intent not working

I seem to be running into a number of problems getting my app going ... grrr
The issue I have at the moment is, I have an Activity on launch of my app that is a searchable facility (via android quick search). The search function works and launches my SearchActivity class where the results as displayed, simples.
BUT, when I attempt a search from the results activity (SearchActivity), the search box pops up and accepts in put, but the intent is never received by SearchActivity or it never launches SearchActivity ... I'm not sure.
Here is the SearchActivity class (this uses the same OnClickListener as the working class):
public class SearchActivity extends MapActivity implements OnGestureListener, OnDoubleTapListener {
public boolean touched = false;
private MapView mapView;
private MapController mapController;
private PlacesItemizedOverlay placesItemizedOverlay;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
Button searchRequest = (Button)findViewById(R.id.searchRequest);
searchRequest.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onSearchRequested();
}
});
mapView.getController();
handleIntent(getIntent());
}
#Override
public boolean onSearchRequested() {
return super.onSearchRequested();
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
// handles a search query
String query = intent.getStringExtra(SearchManager.QUERY);
mapFromQuery(query);
}
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onDoubleTap(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}
The list of taps I don't believe are affecting anything, but I included them just incase. Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.android.example"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".SplashScreen"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main">
<intent-filter>
<action android:name="com.test.android.example.Main" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value=".SearchActivity" />
</activity>
<activity android:name=".SearchActivity"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#layout/searchable"/>
</activity>
</application>
</manifest>
Please help :(
Finally found the solution!!
All that needed to be added was:
#Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
handleIntent(intent);
}
Thats it! Bet someone else is looking for the same result, enjoy!:)
Why not use normal intents with extras? Like so:
Intent intent = new Intent();
intent.setClassName("yourpackage","yourpackage.youractivity");
intent.putExtra("Var name", yourVar); //yourVar can be any type, int, string, etc...
startActivity(intent);

Categories

Resources