Ok I've been looking around for hours. I've tried just about everything and I'm not sure what is going on but I'm getting the issue where it can't find the activity. The activity is in my manifest file. I've tried with .ActivityName and com...ActivityName. I've saved, I've cleaned, I've closed/opened eclipse. It just can't seem to find it. Here's code, maybe I'm missing something?
Manifest file:
<activity android:name=".Activity2"></activity>
Listener in MainActivity:
View.OnClickListener test = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Activity2.class);
startActivity(intent);
}
};
Activity2:
public class Activity2 extends Activity {
ImageButton btnTest;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
btnTest= (ImageButton)findViewById(R.id.btnDepressed);
btnTest.setOnClickListener(GoTest);
}
...etc.
I've read so many articles on this from here and other sites. I've watched videos.. I simply don't see what I'm doing wrong. The activity is defined in the manifest, I've tried the intent with the above code, as well as with:
new Intent("com.blah.blah.Activity2");
Neither is working. Any ideas?
try this
<activity android:name=".Activity2"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.blah.blah.Activity2" />
</intent-filter>
</activity>
from activity code you have to do some thing like this
Intent intent = new Intent();
intent.setAction("com.blah.blah.Activity2");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);`
Check your package com.blah.blah for Activity2.
Inside your AndroidManifest.xml you should define your package as:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blah.blah"
...
And define your activity as:
<activity
android:name="com.blah.blah.Activity2">
</activity>
OR
<activity
android:name=".Activity2">
</activity>
Related
I was learning concept of broadcast receivers and I wanted to make a project with which I can demonstrate triggering specific implicit receiver class which is kind of explicit broadcast.
I made 2 apps the sender app and receiver app for demonstration :
BROADCAST SENDER's MAIN ACTIVITY :
public class MainActivity extends AppCompatActivity {
//Declaring our views
TextView senderTextView;
Button sendButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initializing views
senderTextView = findViewById(R.id.senderTextView);
sendButton = findViewById(R.id.sendButton);
//Setting onClick Listener on sendButton
sendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Calling Broadcast Method
Broadcast();
}
});
}
//Broadcast Method
private void Broadcast(){
//Creating private broadcast intent
Intent intent = new Intent("com.example.broadcastreceiver.PRIVATE_BROADCAST");
//Here we are going to find all the apps in our mobile that have registered for this broadcast in their manifests
//This will help us to find packages or apps registered for com.example.PRIVATE_BROADCAST action
PackageManager packageManager = getPackageManager();
//queryBroadcastReceivers of package manager will query all the receivers having intent filter for "com.example.PRIVATE_BROADCAST" action
//and store receivers info in resolveInfoList
List<ResolveInfo> resolveInfoList = packageManager.queryBroadcastReceivers(intent,0);
//Now we will iterate over this list to find our specific receiver and trigger it
//for each info in resolveInfoList
for (ResolveInfo info : resolveInfoList){
//if info's receiver class name is com.example.broadcastreceiver.CustomBroadcastReceiver (which is our receiver class in receiver app)
if(info.activityInfo.name.equals("com.example.broadcastreceiver.CustomBroadcastReceiver")){
//then use this info to get package name and receiver class name to make a componentName
ComponentName componentName = new ComponentName(info.activityInfo.packageName,info.activityInfo.name);
//now set this componentName to our intent
intent.setComponent(componentName);
}
}
//Sending our private broadcast to our android mobile
sendBroadcast(intent);
//setting textView
senderTextView.setText("Broadcast Sent!");
}
BROADCAST RECEIVER'S MANIFEST :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.broadcastreceiver">
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.BroadcastReceiver"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".CustomBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.example.broadcastreceiver.PRIVATE_BROADCAST"/>
</intent-filter>
</receiver>
</application>
</manifest>
BROADCAST RECEIVER's CustomBroadcastReceiver Class :
public class CustomBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//Making a toast when broadcast is received
Toast.makeText(context, "Broadcast Receiver App : Custom Broadcast Receiver Triggered" , Toast.LENGTH_SHORT).show();
}
BroadcastReceiver's Mainactivity has nothing.
This should trigger the toast in CustomBroadcastReciever class of Broadcast Receiver App but due to some reason toast is not appearing please help.
Thanks in advance :)
Just created a demo app that works:
In app1, send the broadcast like this (obviously change package names):
public void sendBroadcast() {
Intent broadcastIntent = new Intent("com.example.testimplicitbroadcastreceiver.PRIVATE_BROADCAST");
broadcastIntent.setComponent(new ComponentName("com.example.testimplicitbroadcastreceiver",
"com.example.testimplicitbroadcastreceiver.CustomBroadcastReceiver"));
sendBroadcast(broadcastIntent);
}
In app2's manifest, register the BroadcastReceiver:
<receiver
android:name=".CustomBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.example.testimplicitbroadcastreceiver.PRIVATE_BROADCAST"/>
</intent-filter>
</receiver>
Voila, now app2 will receive your Broadcast in the CustomBroadcastReceiver.
I want to change incoming call screen to my own layout. I used a sample to do this for me. but it sometimes appear(when calling) and sometimes not. I do not know how to solve this problem that my own layout appear every time.
This is my codes.
public class PhoneStateReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
Intent i = new Intent();
i.setClass(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
and this is the manifest.
<receiver android:name="com.example.changescreen.PhoneStateReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
Try adding a delay before starting your activity.
The Problem is your activity is being overlapped by the default activity
I'm writing a simple app using Android studio which open main acitivity and when I click on button it make new intent in order to launch URL in web browser. The code to open URL is:
String Link=listURL.get((int) id).toString();
Uri uriUrl = Uri.parse(Link);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
It works fine, it shows the web results and I can come back to the main activity using the back button. All worked fine until I've decided to use a splash screen.
After implemented a splash screen, when I click on back button in web brower, it exit from app. It doesn't come back no more in the "main" activity.
here my manifest.xml:
If i try to remove the .splashscreen stanza, all works fine. I have'nt the splash screen but all works. Could be a manifest's properties problem?
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".SplashScreen"
android:theme="#android:style/Theme.Translucent"
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=".MyActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Here my splash screen:
public class SplashScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timerThread = new Thread(){
public void run(){
try{
sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent intent = new Intent(SplashScreen.this,MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
};
timerThread.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
thanks
Giovanni
call finish() after execution of statement causes starting MyActivity
Intent intent = new Intent(SplashScreen.this,MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Avoid calling finish() in onPause().
delete finish() from onpause and write your timer code inside onresume it will work
Ex:
onresume()
{
Thread timerThread = new Thread(){
public void run(){
try{
sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent intent = new Intent(SplashScreen.this,MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
};
timerThread.start();
}
i think its works for you
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
after intent
finish();
Thank you for your suggestions but it didn't solved.
I've solved using another new and not predefined activity and all works fine.
Giovanni
sorry for asking dumb questions, java and Android are both new to me ;)
My problem: I can't switch between two activities in a very simple app. I tried solutions described in similar topics but it didn't work.
So this is my 1st Activity (I didn't paste the imports):
public class OneActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void OnStart(){
Button Btn = (Button)findViewById(R.id.btnNext);
Btn.setOnClickListener(new OnClickListener() {
public void onClick(View Button) {
Intent myIntent = new Intent(OneActivity.this, UserInput.class);
OneActivity.this.startActivity(myIntent);
}
});
}
}
The second Activity is very simple - it is just supposed to load a layout called userinput.xml:
public class UserInput extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userinput);
}
}
The application part of the Manifest looks like following:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".OneActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity
android:name=".UserInput"
android:label="#string/app_name" />
</activity>
</application>
When I run the app and click the button nothing happens. Where could be the problem?
// Alright, I have put the code into the onCreate() method so it now looks like following:
public class OneActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button Btn = (Button)findViewById(R.id.btnNext);
Btn.setOnClickListener(new OnClickListener() {
public void onClick(View Button) {
Intent myIntent = new Intent(OneActivity.this, UserInput.class);
OneActivity.this.startActivity(myIntent);
}
});
}
}
Now the app crashes (force close) anytime I click the Next button.
You define your onStart() function with a capital 'O'. That is why the function is never called.
Your onStart():
public void OnStart(){ ... }
How it should be:
// Note the lowercase 'o' in onStart
public void onStart(){ ... }
Also note that having an #Override above the function name when you want to override a method will help prevent making these mistakes, as Eclipse (or whatever IDE you use) will tell you that you are not actually overriding a function.
Write below code in onCreate() method:
Button Btn = (Button)findViewById(R.id.btnNext);
Btn.setOnClickListener(new OnClickListener() {
public void onClick(View Button) {
Intent myIntent = new Intent(OneActivity.this, UserInput.class);
startActivity(myIntent);
}
});
2nd correction for android manifest file:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".OneActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <!-- closed here -->
<activity
android:name=".UserInput"
android:label="#string/app_name" />
</application>
You can move all the code in onCreate() and simplify like this
startActivity(new Intent(this, UserInput.class));
But I don't understand why you start another activity like this
Check it following line
Intent myIntent = new Intent(OneAvtivity.this, SecondActivity.class);
startActivity(myIntent);
Your Class name is UserIpnout and you write SecondActivity.class
Intent myIntent = new Intent(OneAvtivity.this, UserInput .class);
startActivity(myIntent);
I am unable to receive broadcast intent, although I have registered it . Can you please spot the error. Thank you very much. I tried with log messages, but it never reached onReceive() of broadcast receiver class.
Here is Class from which I send Broadcast
public class ServiceDemoActivity extends Activity {
Button startButton,stopButton,pauseButton;
final String musicIntentAction="com.CompetenceProject.Musicfilter";
Intent musicIntent=new Intent();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startButton=(Button)findViewById(R.id.startButton);
stopButton=(Button)findViewById(R.id.Stopbutton);
pauseButton=(Button)findViewById(R.id.Pausebutton);
musicIntent.setAction(musicIntentAction);
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.w("DEBUG", "Came to StartButton");
musicIntent.putExtra("Message","start");
sendBroadcast(musicIntent);
Toast.makeText(getApplicationContext(), "starBroadcaseSent",1000).show();
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.w("DEBUG", "Came to StopButton");
musicIntent.putExtra("Message","stop");
sendBroadcast(musicIntent);
Toast.makeText(getApplicationContext(), "stopBroadcaseSent",1000).show();
}
});
pauseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.w("DEBUG", "Came to PauseButton");
musicIntent.putExtra("Message","pause");
sendBroadcast(musicIntent);
Toast.makeText(getApplicationContext(), "PauseBroadcaseSent",1000).show();
}
});
}
}
Here is Class that listens to broadcast:
public class MusicBroadcsatListener extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.w("DEBUG", "Came to BroadCast OnRecieve");
Bundle extras = intent.getExtras();
String res=extras.getString("Message");
Toast.makeText(context, "Intent Extra is "+res, 1000).show();
Log.v("BroadCase","true");
System.out.println("Broadcast came here");
if(res.equalsIgnoreCase("start"))
Toast.makeText(context, "start", 1000).show();
else if(res.equalsIgnoreCase("pause"))
Toast.makeText(context, "pause", 1000).show();
else if (res.equalsIgnoreCase("stop"))
Toast.makeText(context, "stop", 1000).show();
}
}
This is the Androidmanifest:
<activity
android:label="#string/app_name"
android:name=".ServiceDemoActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<receiver android:name=".MusicBroadcsatListener" android:enabled="true">
<intent-filter>
<action android:name="com.CompetenceProject.Musicfilter"></action>
</intent-filter>
</receiver>
Thank you.
First thing I would do is check this typo you have in your Manifest.xml file. I can't see the name of your receiver class here, but if its name has "broadcast" in it, you spelled "broadcast" wrong -- "MusicBroadcsatListenter" -- in your manifest.xml file:
<receiver android:name=".MusicBroadcsatListener" android:enabled="true">
To test, you have to execute the broadcast receiver before you execute the activity -- on the same device -- because the receiver needs to be running. I'm not sure the best way to do this, but out of curiosity I copied and pasted your code into Eclipse into two separate projects -- one for the receiver and one for the activity. I executed the receiver project, then the activity project, and your code worked fine. If someone has a better way to test this, please post as I'd like to know too.
I found the solution by myself. I had put the receiver inside the activity tag. Thank you all for the support.