-2

喜下面的代码是接收机类的android广播接收机通知

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 


public class AlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.e("the time is right","yay!"); 
     Intent i = new Intent(context.getApplicationContext() , AlarmServie.class); 
     context.startService(i); 
    } 
} 

下面的代码是我的服务类

import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.support.annotation.Nullable; 
import android.support.v4.app.NotificationCompat; 
import android.support.v4.app.TaskStackBuilder; 
import android.util.Log; 

public class AlarmServie extends Service { 
    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     Log.e("hello","hi"); 
     return null; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
    super.onStartCommand(intent, flags, startId); 
     Log.e("onCreate:","came"); 
    /* NotificationManager notifyman = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     Intent main_activity = new Intent(this.getApplicationContext(), MainActivity.class); 
     PendingIntent o = PendingIntent.getActivity(this, 0, main_activity, 0); 

     /*Notification noti = new Notification.Builder(this) 
       .setContentTitle("Reminder to Pill") 
       .setContentText("Click for info") 
       .setAutoCancel(true) 
       .setContentIntent(o)v 
       .build();*/ 
     NotificationManager nm = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); 
     Intent in = new Intent(this.getApplicationContext(), MainActivity.class); 
     PendingIntent pending = PendingIntent.getActivity(this, 0, in, 0); 
     NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(AlarmServie.this); 
     mBuilder.setContentTitle("Pill Reminder"); 
     mBuilder.setContentText("CLick here to View"); 
     //mBuilder.setSound(sound); 

     TaskStackBuilder ts=TaskStackBuilder.create(this); 
     ts.addParentStack(MainActivity.class); 
     nm.notify(9999,mBuilder.build()); 
     return START_STICKY; 
    }} 

下面的代码是我的清单

<uses-permission android:name="android.permission.VIBRATE"/> 
    <uses-permission android:name="android.permission.WAKE_LOCK"/> 
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 


    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 



      </intent-filter> 
     </activity> 


     <receiver 
        android:name=".AlarmReceiver" 
        android:enabled="true"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED"/> 
      </intent-filter> 

     </receiver> 

     <activity android:name=".Set"> 

     </activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for 
    App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. --> 
     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 


      <service 
       android:name=".AlarmServie" 
       android:enabled="true"/> 




    </application> 

</manifest> 

我用一个文本框和一个计时器按钮从用户获取文本和时间,并将其保存在数据库中匹配系统时间通知一定要来,但它不来任何人都可以说,为什么这happens.clearly通过详细的代码

解释解释Ÿ发生这种情况,这是我的主要活动

import android.app.AlarmManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.TextView; 

import com.google.android.gms.common.api.GoogleApiClient; 

import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.List; 


public class MainActivity extends AppCompatActivity { 
    Sqlitedb dbobj1; 
    int gh; 
    AlarmManager am; 


    Calendar c; 
    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     dbobj1 = new Sqlitedb(MainActivity.this); 
     gh = (int) dbobj1.number(); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     Calendar c = Calendar.getInstance(); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(MainActivity.this, Set.class); 
       startActivity(intent); 
      } 
     }); 
     Intent my_intent = new Intent(MainActivity.this, AlarmServie.class); 


     am = (AlarmManager) getSystemService(ALARM_SERVICE); 
     Date dt = new Date(); 
     int hour=dt.getHours(); 
     int min=dt.getMinutes(); 
     PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, my_intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     Cursor t = dbobj1.recenttime(hour,min); 
     t.moveToFirst(); 
     c.set(Calendar.HOUR_OF_DAY, t.getInt(0)); 
     c.set(Calendar.MINUTE, t.getInt(1)); 
     am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi); 


     populate(); 

    } 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     //client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    //oncreate ends here 


    class customAdabter extends ArrayAdapter<setget> { 
     Context _context; 
     List<setget> _li; 

     public customAdabter(Context con, int layout, List<setget> li) { 
      super(con, layout, li); 
      _context = con; 
      _li = li; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      gh++; 
      LayoutInflater layu = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      View v = layu.inflate(R.layout.item, null); 


      TextView txt = (TextView) v.findViewById(R.id.item_textview1); 
      TextView txt1 = (TextView) v.findViewById(R.id.item_textview2); 
      Button btt = (Button) v.findViewById(R.id.item_button1); 
      btt.setId(gh); 

      btt.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        int r = v.getId(); 
        dbobj1.deleteRow(r); 

        populate(); 

       } 
      }); 

      setget dataobj = _li.get(position); 

      txt.setText(dataobj.getPn()); 
      txt1.setText(dataobj.getPt()); 

      return v; 
     } 
    } 


    public void populate() { 
     List<setget> li = new ArrayList<setget>(); 

     for (int i = 1; i <= dbobj1.number(); i++) { 
      setget obj = new setget(); 
      Cursor c = dbobj1.getRow(i); 
      String name = c.getString(c.getColumnIndex("pillname")); 
      String time = c.getString(c.getColumnIndex("pilltime")); 
      obj.setPn(name); 
      obj.setPt(time); 
      c.close(); 
      li.add(obj); 
     } 
     ListView lv = (ListView) findViewById(R.id.listview); 
     customAdabter customobj = new customAdabter(MainActivity.this, R.layout.item, li); 
     lv.setAdapter(customobj); 

    } 
} 

这是logcat的

09-19 11:30:32.267 19426-19426/com.example.ashwin.pillreminder11 D/ActivityThread: BDC-Calling onReceive: intent=Intent { flg=0x114 cmp=com.example.ashwin.pillreminder11/.AlarmReceiver (has extras) }, [email protected] 

09-19 11:30:32.267 19426-19426/com.example.ashwin.pillreminder11 E/the time is right: yay! 

09-19 11:30:32.268 19426-19426/com.example.ashwin.pillreminder11 D/ActivityThread: BDC-RECEIVER handled : 0/ReceiverData{intent=Intent { flg=0x114 cmp=com.example.ashwin.pillreminder11/.AlarmReceiver (has extras) } packageName=com.example.ashwin.pillreminder11 resultCode=0 resultData=null resultExtras=null} 
+0

@Mike M.这两个通知和警报不comecoming.can你说为什么会发生这种情况.... –

+0

@Mike M.是我没有看到日志输出。我该怎么做现在可以解释它与代码 –

+0

@Mike M.我已更新我的代码检查它 –

回答

0
<receiver 
     android:name=".AlarmReceiver" 
     android:enabled="true"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"/> 
     </intent-filter> 
</receiver> 

将其更改为

<receiver 
     android:name=".AlarmReceiver" 
     android:enabled="true"> 
</receiver> 

并使用不同的广播接收器接收BOOT_COMPLETED广播并重置所有报警。

+0

问题是接收器类没有移动到服务类 –

+0

提供logcat输出 –

+0

E/MultiWindowProxy:getServiceInstance失败! –