Autore Topic: Problemi con BroadcastReceiver  (Letto 260 volte)

Offline oberdank

  • Utente normale
  • ***
  • Post: 287
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Samsung Galaxy Core LTE
  • Play Store ID:
    BuzzOne
  • Sistema operativo:
    Windows 8.1
Problemi con BroadcastReceiver
« il: 01 Settembre 2014, 17:59:39 CEST »
0
Sono alle prese con un nuovo problema con Android, il listato "MainActivity.java OK" funziona, mentre invece il listato "MainActivity.java ERRORE" mi da errore; mi appare una finestra intitolata "Android Launch" con il testo "Your project contains error(s), please fix them before running your application.".

DOVE SBAGLIO?
Le classi mi sembra di averle importate tutte.
Il pannello "Console" di Eclipse e' completamente vuoto.


********** MainActivity.java OK *********
package com.prove.provaproject_14;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.content.*;
import android.view.*;
import android.widget.*;

public class MainActivity extends Activity {

   /*
   // Creare un Receiver
   private BroadcastReceiver simpleReceiver=new BroadcastReceiver()
   {
      public void onReceive(Context context,Intent intent)
      {
         if (intent.getAction().equals(Intent.ACTION_AIRPLANE_MODE_CHANGED))
         {
            Toast.makeText(context, "testo" , Toast.LENGTH_LONG).show();
         }
      }
   }
   */
   
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      //IntentFilter intentFilter=new IntentFilter();
      //intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
      //registerReceiver(simpleReceiver,intentFilter);
   }

   @Override
   public void onDestroy()
   {
      super.onDestroy();
      //unregisterReceiver(imageReceiver);
      
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      if (id == R.id.action_settings) {
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
   
   @Override
   public boolean onKeyDown(int keyCode,KeyEvent event)
   {
      if (keyCode==KeyEvent.KEYCODE_1)
      {
         Intent intent=new Intent(MainActivity.this,SecondaActivity.class);
         startActivity(intent);
         return(true);
      }
      if (keyCode==KeyEvent.KEYCODE_2)
      {
         Intent actionStartIntent=new Intent("com.haseman.PURPLE_PONY_POWER");
         //Intent actionStartIntent=new Intent("provaIntent");
         startActivity(actionStartIntent);
      }
      return(super.onKeyDown(keyCode, event));
   }
}


********** MainActivity.java ERRORE *********
package com.prove.provaproject_14;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.content.*;
import android.view.*;
import android.widget.*;

public class MainActivity extends Activity {

   
   // Creare un Receiver
   private BroadcastReceiver simpleReceiver=new BroadcastReceiver()
   {
      public void onReceive(Context context,Intent intent)
      {
         if (intent.getAction().equals(Intent.ACTION_AIRPLANE_MODE_CHANGED))
         {
            Toast.makeText(context, "testo" , Toast.LENGTH_LONG).show();
         }
      }
   }
   
   
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      IntentFilter intentFilter=new IntentFilter();
      intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
      registerReceiver(simpleReceiver,intentFilter);
   }

   @Override
   public void onDestroy()
   {
      super.onDestroy();
      //unregisterReceiver(imageReceiver);
      
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      if (id == R.id.action_settings) {
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
   
   @Override
   public boolean onKeyDown(int keyCode,KeyEvent event)
   {
      if (keyCode==KeyEvent.KEYCODE_1)
      {
         Intent intent=new Intent(MainActivity.this,SecondaActivity.class);
         startActivity(intent);
         return(true);
      }
      if (keyCode==KeyEvent.KEYCODE_2)
      {
         Intent actionStartIntent=new Intent("com.haseman.PURPLE_PONY_POWER");
         //Intent actionStartIntent=new Intent("provaIntent");
         startActivity(actionStartIntent);
      }
      return(super.onKeyDown(keyCode, event));
   }
}

Offline bradipao

  • Moderatore globale
  • Utente storico
  • *****
  • Post: 4043
  • keep it simple
  • Respect: +567
    • Github
    • Google+
    • bradipao
    • Mostra profilo
  • Dispositivo Android:
    Nexus 5
  • Play Store ID:
    Bradipao
  • Sistema operativo:
    W7
Re:Problemi con BroadcastReceiver
« Risposta #1 il: 01 Settembre 2014, 18:13:49 CEST »
0
Per cortesia formatta il codice con l'apposito marcatore ( ).
NON rispondo a domande nei messaggi privati
Bradipao @ Play Store